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
11 * \author Jürgen Spitzmüller
13 * Full author contact details are available in file CREDITS.
18 #include "BufferList.h"
20 #include "support/gettext.h"
22 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/convert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxlib.h"
29 #include "support/Systemcall.h"
30 #include "support/os.h"
32 #include <boost/regex.hpp>
43 using support::absolutePath;
44 using support::bformat;
45 using support::changeExtension;
46 using support::contains;
47 using support::FileName;
48 using support::findtexfile;
49 using support::getcwd;
50 using support::makeAbsPath;
51 using support::onlyFilename;
52 using support::prefixIs;
53 using support::quoteName;
54 using support::removeExtension;
56 using support::rsplit;
59 using support::suffixIs;
60 using support::Systemcall;
63 namespace os = support::os;
65 // TODO: in no particular order
66 // - get rid of the call to
67 // BufferList::updateIncludedTeXfiles, this should either
68 // be done before calling LaTeX::funcs or in a completely
70 // - the makeindex style files should be taken care of with
71 // the dependency mechanism.
72 // - makeindex commandline options should be supported
73 // - somewhere support viewing of bibtex and makeindex log files.
74 // - we should perhaps also scan the bibtex log file
78 docstring runMessage(unsigned int count)
80 return bformat(_("Waiting for LaTeX run number %1$d"), count);
89 void TeXErrors::insertError(int line, docstring const & error_desc,
90 docstring const & error_text)
92 Error newerr(line, error_desc, error_text);
93 errors.push_back(newerr);
97 bool operator==(Aux_Info const & a, Aux_Info const & o)
99 return a.aux_file == o.aux_file &&
100 a.citations == o.citations &&
101 a.databases == o.databases &&
102 a.styles == o.styles;
106 bool operator!=(Aux_Info const & a, Aux_Info const & o)
116 LaTeX::LaTeX(string const & latex, OutputParams const & rp,
118 : cmd(latex), file(f), runparams(rp)
121 if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
122 depfile = FileName(file.absFilename() + ".dep-pdf");
124 FileName(changeExtension(file.absFilename(), ".pdf"));
126 depfile = FileName(file.absFilename() + ".dep");
128 FileName(changeExtension(file.absFilename(), ".dvi"));
133 void LaTeX::deleteFilesOnError() const
135 // currently just a dummy function.
137 // What files do we have to delete?
139 // This will at least make latex do all the runs
140 depfile.removeFile();
142 // but the reason for the error might be in a generated file...
145 FileName const bbl(changeExtension(file.absFilename(), ".bbl"));
149 FileName const ind(changeExtension(file.absFilename(), ".ind"));
153 FileName const nls(changeExtension(file.absFilename(), ".nls"));
156 // nomencl file (old version of the package)
157 FileName const gls(changeExtension(file.absFilename(), ".gls"));
160 // Also remove the aux file
161 FileName const aux(changeExtension(file.absFilename(), ".aux"));
166 int LaTeX::run(TeXErrors & terr)
167 // We know that this function will only be run if the lyx buffer
168 // has been changed. We also know that a newly written .tex file
169 // is always different from the previous one because of the date
170 // in it. However it seems safe to run latex (at least) on time
171 // each time the .tex file changes.
173 int scanres = NO_ERRORS;
174 unsigned int count = 0; // number of times run
175 num_errors = 0; // just to make sure.
176 unsigned int const MAX_RUN = 6;
177 DepTable head; // empty head
178 bool rerun = false; // rerun requested
180 // The class LaTeX does not know the temp path.
181 theBufferList().updateIncludedTeXfiles(getcwd().absFilename(),
184 // Never write the depfile if an error was encountered.
187 // first check if the file dependencies exist:
188 // ->If it does exist
189 // check if any of the files mentioned in it have
190 // changed (done using a checksum).
192 // run latex once and
193 // remake the dependency file
194 // -> if not changed:
195 // just return there is nothing to do for us.
196 // ->if it doesn't exist
198 // run latex once (we need to run latex once anyway) and
199 // remake the dependency file.
202 bool had_depfile = depfile.exists();
203 bool run_bibtex = false;
204 FileName const aux_file(changeExtension(file.absFilename(), "aux"));
207 LYXERR(Debug::DEPEND, "Dependency file exists");
208 // Read the dep file:
209 had_depfile = head.read(depfile);
213 // Update the checksums
215 // Can't just check if anything has changed because it might
216 // have aborted on error last time... in which cas we need
217 // to re-run latex and collect the error messages
218 // (even if they are the same).
219 if (!output_file.exists()) {
220 LYXERR(Debug::DEPEND,
221 "re-running LaTeX because output file doesn't exist.");
222 } else if (!head.sumchange()) {
223 LYXERR(Debug::DEPEND, "return no_change");
226 LYXERR(Debug::DEPEND, "Dependency file has changed");
229 if (head.extchanged(".bib") || head.extchanged(".bst"))
232 LYXERR(Debug::DEPEND,
233 "Dependency file does not exist, or has wrong format");
235 /// We scan the aux file even when had_depfile = false,
236 /// because we can run pdflatex on the file after running latex on it,
237 /// in which case we will not need to run bibtex again.
238 vector<Aux_Info> bibtex_info_old;
240 bibtex_info_old = scanAuxFiles(aux_file);
243 LYXERR(Debug::LATEX, "Run #" << count);
244 message(runMessage(count));
247 scanres = scanLogFile(terr);
248 if (scanres & ERROR_RERUN) {
249 LYXERR(Debug::LATEX, "Rerunning LaTeX");
251 scanres = scanLogFile(terr);
254 if (scanres & ERRORS) {
255 deleteFilesOnError();
256 return scanres; // return on error
259 vector<Aux_Info> const bibtex_info = scanAuxFiles(aux_file);
260 if (!run_bibtex && bibtex_info_old != bibtex_info)
263 // update the dependencies.
264 deplog(head); // reads the latex log
268 // At this point we must run external programs if needed.
269 // makeindex will be run if a .idx file changed or was generated.
270 // And if there were undefined citations or changes in references
271 // the .aux file is checked for signs of bibtex. Bibtex is then run
274 // memoir (at least) writes an empty *idx file in the first place.
275 // A second latex run is needed.
276 FileName const idxfile(changeExtension(file.absFilename(), ".idx"));
277 rerun = idxfile.exists() && idxfile.isFileEmpty();
280 if (head.haschanged(idxfile)) {
282 LYXERR(Debug::LATEX, "Running MakeIndex.");
283 message(_("Running MakeIndex."));
284 // onlyFilename() is needed for cygwin
285 rerun |= runMakeIndex(onlyFilename(idxfile.absFilename()),
288 FileName const nlofile(changeExtension(file.absFilename(), ".nlo"));
289 if (head.haschanged(nlofile))
290 rerun |= runMakeIndexNomencl(file, runparams, ".nlo", ".nls");
291 FileName const glofile(changeExtension(file.absFilename(), ".glo"));
292 if (head.haschanged(glofile))
293 rerun |= runMakeIndexNomencl(file, runparams, ".glo", ".gls");
296 // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
297 if (scanres & UNDEF_CIT || run_bibtex) {
298 // Here we must scan the .aux file and look for
299 // "\bibdata" and/or "\bibstyle". If one of those
300 // tags is found -> run bibtex and set rerun = true;
302 LYXERR(Debug::LATEX, "Running BibTeX.");
303 message(_("Running BibTeX."));
304 updateBibtexDependencies(head, bibtex_info);
305 rerun |= runBibTeX(bibtex_info);
306 } else if (!had_depfile) {
307 /// If we run pdflatex on the file after running latex on it,
308 /// then we do not need to run bibtex, but we do need to
309 /// insert the .bib and .bst files into the .dep-pdf file.
310 updateBibtexDependencies(head, bibtex_info);
314 // we know on this point that latex has been run once (or we just
315 // returned) and the question now is to decide if we need to run
316 // it any more. This is done by asking if any of the files in the
317 // dependency file has changed. (remember that the checksum for
318 // a given file is reported to have changed if it just was created)
319 // -> if changed or rerun == true:
320 // run latex once more and
321 // update the dependency structure
322 // -> if not changed:
323 // we does nothing at this point
325 if (rerun || head.sumchange()) {
328 LYXERR(Debug::DEPEND, "Dep. file has changed or rerun requested");
329 LYXERR(Debug::LATEX, "Run #" << count);
330 message(runMessage(count));
332 scanres = scanLogFile(terr);
333 if (scanres & ERRORS) {
334 deleteFilesOnError();
335 return scanres; // return on error
338 // update the depedencies
339 deplog(head); // reads the latex log
342 LYXERR(Debug::DEPEND, "Dep. file has NOT changed");
346 // The inclusion of files generated by external programs like
347 // makeindex or bibtex might have done changes to pagenumbering,
348 // etc. And because of this we must run the external programs
349 // again to make sure everything is redone correctly.
350 // Also there should be no need to run the external programs any
353 // run makeindex if the <file>.idx has changed or was generated.
354 if (head.haschanged(idxfile)) {
356 LYXERR(Debug::LATEX, "Running MakeIndex.");
357 message(_("Running MakeIndex."));
358 // onlyFilename() is needed for cygwin
359 rerun = runMakeIndex(onlyFilename(changeExtension(
360 file.absFilename(), ".idx")), runparams);
363 // I am not pretty sure if need this twice.
364 if (head.haschanged(nlofile))
365 rerun |= runMakeIndexNomencl(file, runparams, ".nlo", ".nls");
366 if (head.haschanged(glofile))
367 rerun |= runMakeIndexNomencl(file, runparams, ".glo", ".gls");
370 // we will only run latex more if the log file asks for it.
371 // or if the sumchange() is true.
372 // -> rerun asked for:
374 // remake the dependency file
375 // goto 2 or return if max runs are reached.
376 // -> rerun not asked for:
377 // just return (fall out of bottom of func)
379 while ((head.sumchange() || rerun || (scanres & RERUN))
380 && count < MAX_RUN) {
381 // Yes rerun until message goes away, or until
382 // MAX_RUNS are reached.
385 LYXERR(Debug::LATEX, "Run #" << count);
386 message(runMessage(count));
388 scanres = scanLogFile(terr);
389 if (scanres & ERRORS) {
390 deleteFilesOnError();
391 return scanres; // return on error
398 // Write the dependencies to file.
400 LYXERR(Debug::LATEX, "Done.");
405 int LaTeX::startscript()
407 // onlyFilename() is needed for cygwin
408 string tmp = cmd + ' '
409 + quoteName(onlyFilename(file.toFilesystemEncoding()))
410 + " > " + os::nulldev();
412 return one.startscript(Systemcall::Wait, tmp);
416 bool LaTeX::runMakeIndex(string const & f, OutputParams const & runparams,
417 string const & params)
420 "idx file has been made, running makeindex on file " << f);
421 string tmp = lyxrc.index_command + ' ';
423 tmp = subst(tmp, "$$lang", runparams.document_language);
427 one.startscript(Systemcall::Wait, tmp);
432 bool LaTeX::runMakeIndexNomencl(FileName const & file,
433 OutputParams const & runparams,
434 string const & nlo, string const & nls)
436 LYXERR(Debug::LATEX, "Running MakeIndex for nomencl.");
437 message(_("Running MakeIndex for nomencl."));
438 // onlyFilename() is needed for cygwin
439 string const nomenclstr = " -s nomencl.ist -o "
440 + onlyFilename(changeExtension(file.toFilesystemEncoding(), nls));
442 onlyFilename(changeExtension(file.absFilename(), nlo)),
443 runparams, nomenclstr);
447 vector<Aux_Info> const
448 LaTeX::scanAuxFiles(FileName const & file)
450 vector<Aux_Info> result;
452 result.push_back(scanAuxFile(file));
454 string const basename = removeExtension(file.absFilename());
455 for (int i = 1; i < 1000; ++i) {
456 FileName const file2(basename
457 + '.' + convert<string>(i)
461 result.push_back(scanAuxFile(file2));
467 Aux_Info const LaTeX::scanAuxFile(FileName const & file)
470 result.aux_file = file;
471 scanAuxFile(file, result);
476 void LaTeX::scanAuxFile(FileName const & file, Aux_Info & aux_info)
478 LYXERR(Debug::LATEX, "Scanning aux file: " << file);
480 ifstream ifs(file.toFilesystemEncoding().c_str());
482 static regex const reg1("\\\\citation\\{([^}]+)\\}");
483 static regex const reg2("\\\\bibdata\\{([^}]+)\\}");
484 static regex const reg3("\\\\bibstyle\\{([^}]+)\\}");
485 static regex const reg4("\\\\@input\\{([^}]+)\\}");
487 while (getline(ifs, token)) {
488 token = rtrim(token, "\r");
490 // FIXME UNICODE: We assume that citation keys and filenames
491 // in the aux file are in the file system encoding.
492 token = to_utf8(from_filesystem8bit(token));
493 if (regex_match(token, sub, reg1)) {
494 string data = sub.str(1);
495 while (!data.empty()) {
497 data = split(data, citation, ',');
498 LYXERR(Debug::LATEX, "Citation: " << citation);
499 aux_info.citations.insert(citation);
501 } else if (regex_match(token, sub, reg2)) {
502 string data = sub.str(1);
503 // data is now all the bib files separated by ','
504 // get them one by one and pass them to the helper
505 while (!data.empty()) {
507 data = split(data, database, ',');
508 database = changeExtension(database, "bib");
509 LYXERR(Debug::LATEX, "BibTeX database: `" << database << '\'');
510 aux_info.databases.insert(database);
512 } else if (regex_match(token, sub, reg3)) {
513 string style = sub.str(1);
514 // token is now the style file
515 // pass it to the helper
516 style = changeExtension(style, "bst");
517 LYXERR(Debug::LATEX, "BibTeX style: `" << style << '\'');
518 aux_info.styles.insert(style);
519 } else if (regex_match(token, sub, reg4)) {
520 string const file2 = sub.str(1);
521 scanAuxFile(makeAbsPath(file2), aux_info);
527 void LaTeX::updateBibtexDependencies(DepTable & dep,
528 vector<Aux_Info> const & bibtex_info)
530 // Since a run of Bibtex mandates more latex runs it is ok to
531 // remove all ".bib" and ".bst" files.
532 dep.remove_files_with_extension(".bib");
533 dep.remove_files_with_extension(".bst");
534 //string aux = OnlyFilename(ChangeExtension(file, ".aux"));
536 for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
537 it != bibtex_info.end(); ++it) {
538 for (set<string>::const_iterator it2 = it->databases.begin();
539 it2 != it->databases.end(); ++it2) {
540 FileName const file = findtexfile(*it2, "bib");
542 dep.insert(file, true);
545 for (set<string>::const_iterator it2 = it->styles.begin();
546 it2 != it->styles.end(); ++it2) {
547 FileName const file = findtexfile(*it2, "bst");
549 dep.insert(file, true);
555 bool LaTeX::runBibTeX(vector<Aux_Info> const & bibtex_info)
558 for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
559 it != bibtex_info.end(); ++it) {
560 if (it->databases.empty())
564 string tmp = lyxrc.bibtex_command + " ";
565 // onlyFilename() is needed for cygwin
566 tmp += quoteName(onlyFilename(removeExtension(
567 it->aux_file.absFilename())));
569 one.startscript(Systemcall::Wait, tmp);
571 // Return whether bibtex was run
576 int LaTeX::scanLogFile(TeXErrors & terr)
580 int retval = NO_ERRORS;
582 onlyFilename(changeExtension(file.absFilename(), ".log"));
583 LYXERR(Debug::LATEX, "Log file: " << tmp);
584 FileName const fn = FileName(makeAbsPath(tmp));
585 ifstream ifs(fn.toFilesystemEncoding().c_str());
586 bool fle_style = false;
587 static regex file_line_error(".+\\.\\D+:[0-9]+: (.+)");
590 while (getline(ifs, token)) {
591 // MikTeX sometimes inserts \0 in the log file. They can't be
592 // removed directly with the existing string utility
593 // functions, so convert them first to \r, and remove all
594 // \r's afterwards, since we need to remove them anyway.
595 token = subst(token, '\0', '\r');
596 token = subst(token, "\r", "");
599 LYXERR(Debug::LATEX, "Log line: " << token);
604 if (contains(token, "file:line:error style messages enabled"))
607 if (prefixIs(token, "LaTeX Warning:") ||
608 prefixIs(token, "! pdfTeX warning")) {
609 // Here shall we handle different
611 retval |= LATEX_WARNING;
612 LYXERR(Debug::LATEX, "LaTeX Warning.");
613 if (contains(token, "Rerun to get cross-references")) {
615 LYXERR(Debug::LATEX, "We should rerun.");
616 } else if (contains(token, "Citation")
617 && contains(token, "on page")
618 && contains(token, "undefined")) {
621 } else if (prefixIs(token, "Package")) {
623 retval |= PACKAGE_WARNING;
624 if (contains(token, "natbib Warning:")) {
626 if (contains(token, "Citation")
627 && contains(token, "on page")
628 && contains(token, "undefined")) {
631 } else if (contains(token, "run BibTeX")) {
633 } else if (contains(token, "Rerun LaTeX") ||
634 contains(token, "Rerun to get")) {
635 // at least longtable.sty and bibtopic.sty
637 LYXERR(Debug::LATEX, "We should rerun.");
640 } else if (token[0] == '(') {
641 if (contains(token, "Rerun LaTeX") ||
642 contains(token, "Rerun to get")) {
644 LYXERR(Debug::LATEX, "We should rerun.");
647 } else if (prefixIs(token, "! ") ||
648 fle_style && regex_match(token, sub, file_line_error)) {
649 // Ok, we have something that looks like a TeX Error
650 // but what do we really have.
652 // Just get the error description:
654 if (prefixIs(token, "! "))
655 desc = string(token, 2);
658 if (contains(token, "LaTeX Error:"))
659 retval |= LATEX_ERROR;
664 if (!getline(ifs, tmp))
668 } while (!prefixIs(tmp, "l."));
669 if (prefixIs(tmp, "l.")) {
670 // we have a latex error
673 "Package babel Error: You haven't defined the language") ||
675 "Package babel Error: You haven't loaded the option"))
676 retval |= ERROR_RERUN;
677 // get the line number:
679 sscanf(tmp.c_str(), "l.%d", &line);
680 // get the rest of the message:
681 string errstr(tmp, tmp.find(' '));
684 while (!contains(errstr, "l.")
686 && !prefixIs(tmp, "! ")
687 && !contains(tmp, "(job aborted")) {
692 LYXERR(Debug::LATEX, "line: " << line << '\n'
693 << "Desc: " << desc << '\n' << "Text: " << errstr);
694 if (line == last_line)
700 if (line_count <= 5) {
702 // We have no idea what the encoding of
704 // It seems that the output from the
705 // latex compiler itself is pure ASCII,
706 // but it can include bits from the
707 // document, so whatever encoding we
708 // assume here it can be wrong.
709 terr.insertError(line,
710 from_local8bit(desc),
711 from_local8bit(errstr));
716 // information messages, TeX warnings and other
717 // warnings we have not caught earlier.
718 if (prefixIs(token, "Overfull ")) {
719 retval |= TEX_WARNING;
720 } else if (prefixIs(token, "Underfull ")) {
721 retval |= TEX_WARNING;
722 } else if (contains(token, "Rerun to get citations")) {
723 // Natbib seems to use this.
725 } else if (contains(token, "No pages of output")) {
726 // A dvi file was not created
728 } else if (contains(token, "That makes 100 errors")) {
729 // More than 100 errors were reprted
730 retval |= TOO_MANY_ERRORS;
734 LYXERR(Debug::LATEX, "Log line: " << token);
741 bool insertIfExists(FileName const & absname, DepTable & head)
743 if (absname.exists() && !absname.isDirectory()) {
744 head.insert(absname, true);
751 bool handleFoundFile(string const & ff, DepTable & head)
753 // convert from native os path to unix path
754 string foundfile = os::internal_path(trim(ff));
756 LYXERR(Debug::DEPEND, "Found file: " << foundfile);
758 // Ok now we found a file.
759 // Now we should make sure that this is a file that we can
760 // access through the normal paths.
761 // We will not try any fancy search methods to
764 // (1) foundfile is an
765 // absolute path and should
767 if (absolutePath(foundfile)) {
768 LYXERR(Debug::DEPEND, "AbsolutePath file: " << foundfile);
769 // On initial insert we want to do the update at once
770 // since this file cannot be a file generated by
772 FileName absname(foundfile);
773 if (!insertIfExists(absname, head)) {
775 string strippedfile = foundfile;
776 while (contains(strippedfile, " ")) {
777 // files with spaces are often enclosed in quotation
778 // marks; those have to be removed
779 string unquoted = subst(strippedfile, "\"", "");
780 absname.set(unquoted);
781 if (insertIfExists(absname, head))
783 // strip off part after last space and try again
784 string tmp = strippedfile;
785 string const stripoff =
786 rsplit(tmp, strippedfile, ' ');
787 absname.set(strippedfile);
788 if (insertIfExists(absname, head))
794 string onlyfile = onlyFilename(foundfile);
795 FileName absname(makeAbsPath(onlyfile));
798 while (contains(foundfile, ' ')) {
799 if (absname.exists())
803 // files with spaces are often enclosed in quotation
804 // marks; those have to be removed
805 string unquoted = subst(foundfile, "\"", "");
806 absname = makeAbsPath(unquoted);
807 if (absname.exists())
809 // strip off part after last space and try again
811 string const stripoff =
812 rsplit(foundfile, strippedfile, ' ');
813 foundfile = strippedfile;
814 onlyfile = onlyFilename(strippedfile);
815 absname = makeAbsPath(onlyfile);
819 // (2) foundfile is in the tmpdir
820 // insert it into head
821 if (absname.exists() && !absname.isDirectory()) {
822 // FIXME: This regex contained glo, but glo is used by the old
823 // version of nomencl.sty. Do we need to put it back?
824 static regex const unwanted("^.*\\.(aux|log|dvi|bbl|ind)$");
825 if (regex_match(onlyfile, unwanted)) {
826 LYXERR(Debug::DEPEND, "We don't want " << onlyfile
827 << " in the dep file");
828 } else if (suffixIs(onlyfile, ".tex")) {
829 // This is a tex file generated by LyX
830 // and latex is not likely to change this
832 LYXERR(Debug::DEPEND, "Tmpdir TeX file: " << onlyfile);
833 head.insert(absname, true);
835 LYXERR(Debug::DEPEND, "In tmpdir file:" << onlyfile);
836 head.insert(absname);
840 LYXERR(Debug::DEPEND, "Not a file or we are unable to find it.");
846 bool checkLineBreak(string const & ff, DepTable & head)
848 if (!contains(ff, '.'))
851 // if we have a dot, we let handleFoundFile decide
852 return handleFoundFile(ff, head);
858 void LaTeX::deplog(DepTable & head)
860 // This function reads the LaTeX log file end extracts all the
861 // external files used by the LaTeX run. The files are then
862 // entered into the dependency file.
864 string const logfile =
865 onlyFilename(changeExtension(file.absFilename(), ".log"));
867 static regex const reg1("File: (.+).*");
868 static regex const reg2("No file (.+)(.).*");
869 static regex const reg3("\\\\openout[0-9]+.*=.*`(.+)(..).*");
870 // If an index should be created, MikTex does not write a line like
871 // \openout# = 'sample.idx'.
872 // but instead only a line like this into the log:
873 // Writing index file sample.idx
874 static regex const reg4("Writing index file (.+).*");
875 // files also can be enclosed in <...>
876 static regex const reg5("<([^>]+)(.).*");
877 static regex const regoldnomencl("Writing glossary file (.+).*");
878 static regex const regnomencl("Writing nomenclature file (.+).*");
879 // If a toc should be created, MikTex does not write a line like
880 // \openout# = `sample.toc'.
881 // but only a line like this into the log:
883 // This line is also written by tetex.
884 // This line is not present if no toc should be created.
885 static regex const miktexTocReg("\\\\tf@toc=\\\\write.*");
886 static regex const reg6(".*\\([^)]+.*");
888 FileName const fn = makeAbsPath(logfile);
889 ifstream ifs(fn.toFilesystemEncoding().c_str());
892 // Ok, the scanning of files here is not sufficient.
893 // Sometimes files are named by "File: xxx" only
894 // So I think we should use some regexps to find files instead.
895 // Note: all file names and paths might contains spaces.
896 bool found_file = false;
899 // MikTeX sometimes inserts \0 in the log file. They can't be
900 // removed directly with the existing string utility
901 // functions, so convert them first to \r, and remove all
902 // \r's afterwards, since we need to remove them anyway.
903 token = subst(token, '\0', '\r');
904 token = subst(token, "\r", "");
905 if (token.empty() || token == ")") {
910 // Sometimes, filenames are broken across lines.
911 // We care for that and save suspicious lines.
912 // Here we exclude some cases where we are sure
913 // that there is no continued filename
914 if (!lastline.empty()) {
915 static regex const package_info("Package \\w+ Info: .*");
916 static regex const package_warning("Package \\w+ Warning: .*");
917 if (prefixIs(token, "File:") || prefixIs(token, "(Font)")
918 || prefixIs(token, "Package:")
919 || prefixIs(token, "Language:")
920 || prefixIs(token, "LaTeX Info:")
921 || prefixIs(token, "LaTeX Font Info:")
922 || prefixIs(token, "\\openout[")
923 || prefixIs(token, "))")
924 || regex_match(token, package_info)
925 || regex_match(token, package_warning))
929 if (!lastline.empty())
930 // probably a continued filename from last line
931 token = lastline + token;
932 if (token.length() > 255) {
933 // string too long. Cut off.
934 token.erase(0, token.length() - 251);
939 // FIXME UNICODE: We assume that the file names in the log
940 // file are in the file system encoding.
941 token = to_utf8(from_filesystem8bit(token));
943 // (1) "File: file.ext"
944 if (regex_match(token, sub, reg1)) {
946 found_file = checkLineBreak(sub.str(1), head);
948 if (suffixIs(token, ")"))
949 // no line break for sure
950 // pretend we've been succesfully searching
952 // (2) "No file file.ext"
953 } else if (regex_match(token, sub, reg2)) {
954 // file names must contains a dot, line ends with dot
955 if (contains(sub.str(1), '.') && sub.str(2) == ".")
956 found_file = handleFoundFile(sub.str(1), head);
958 // we suspect a line break
960 // (3) "\openout<nr> = `file.ext'."
961 } else if (regex_match(token, sub, reg3)) {
962 // search for closing '. at the end of the line
963 if (sub.str(2) == "\'.")
964 found_file = handleFoundFile(sub.str(1), head);
966 // probable line break
968 // (4) "Writing index file file.ext"
969 } else if (regex_match(token, sub, reg4))
971 found_file = checkLineBreak(sub.str(1), head);
973 else if (regex_match(token, sub, reg5)) {
974 // search for closing '>' and dot ('*.*>') at the eol
975 if (contains(sub.str(1), '.') && sub.str(2) == ">")
976 found_file = handleFoundFile(sub.str(1), head);
978 // probable line break
980 // (6) "Writing nomenclature file file.ext"
981 } else if (regex_match(token, sub, regnomencl) ||
982 regex_match(token, sub, regoldnomencl))
984 found_file = checkLineBreak(sub.str(1), head);
985 // (7) "\tf@toc=\write<nr>" (for MikTeX)
986 else if (regex_match(token, sub, miktexTocReg))
987 found_file = handleFoundFile(onlyFilename(changeExtension(
988 file.absFilename(), ".toc")), head);
990 // not found, but we won't check further
991 // pretend we've been succesfully searching
995 // note that we can have several of these on one line
996 // this must be queried separated, because of
997 // cases such as "File: file.ext (type eps)"
998 // where "File: file.ext" would be skipped
999 if (regex_match(token, sub, reg6)) {
1000 // search for strings in (...)
1001 static regex reg6_1("\\(([^()]+)(.).*");
1003 string::const_iterator first = token.begin();
1004 string::const_iterator end = token.end();
1006 while (regex_search(first, end, what, reg6_1)) {
1007 // if we have a dot, try to handle as file
1008 if (contains(what.str(1), '.')) {
1009 first = what[0].second;
1010 if (what.str(2) == ")") {
1011 handleFoundFile(what.str(1), head);
1012 // since we had a closing bracket,
1013 // do not investigate further
1016 // if we have no closing bracket,
1017 // try to handle as file nevertheless
1018 found_file = handleFoundFile(
1019 what.str(1) + what.str(2), head);
1021 // if we do not have a dot, check if the line has
1022 // a closing bracket (else, we suspect a line break)
1023 else if (what.str(2) != ")") {
1024 first = what[0].second;
1027 // we have a closing bracket, so the content
1028 // is not a file name.
1029 // no need to investigate further
1030 // pretend we've been succesfully searching
1031 first = what[0].second;
1038 // probable linebreak:
1042 // no linebreak: reset
1043 lastline = string();
1046 // Make sure that the main .tex file is in the dependency file.
1047 head.insert(file, true);