]> git.lyx.org Git - lyx.git/blob - src/LaTeX.cpp
Revert qprocess code. Revisions reverted: 22026, 22030, 22044, 22048,
[lyx.git] / src / LaTeX.cpp
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.
5  *
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
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "BufferList.h"
19 #include "LaTeX.h"
20 #include "support/gettext.h"
21 #include "LyXRC.h"
22 #include "support/debug.h"
23 #include "DepTable.h"
24
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"
31
32 #include <boost/regex.hpp>
33
34 #include <fstream>
35
36 using boost::regex;
37 using boost::smatch;
38
39 using namespace std;
40
41 namespace lyx {
42
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;
55 using support::rtrim;
56 using support::rsplit;
57 using support::split;
58 using support::subst;
59 using support::suffixIs;
60 using support::Systemcall;
61 using support::trim;
62
63 namespace os = support::os;
64
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
69 //   different way.
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
75
76 namespace {
77
78 docstring runMessage(unsigned int count)
79 {
80         return bformat(_("Waiting for LaTeX run number %1$d"), count);
81 }
82
83 } // anon namespace
84
85 /*
86  * CLASS TEXERRORS
87  */
88
89 void TeXErrors::insertError(int line, docstring const & error_desc,
90                             docstring const & error_text)
91 {
92         Error newerr(line, error_desc, error_text);
93         errors.push_back(newerr);
94 }
95
96
97 bool operator==(Aux_Info const & a, Aux_Info const & o)
98 {
99         return a.aux_file == o.aux_file &&
100                 a.citations == o.citations &&
101                 a.databases == o.databases &&
102                 a.styles == o.styles;
103 }
104
105
106 bool operator!=(Aux_Info const & a, Aux_Info const & o)
107 {
108         return !(a == o);
109 }
110
111
112 /*
113  * CLASS LaTeX
114  */
115
116 LaTeX::LaTeX(string const & latex, OutputParams const & rp,
117              FileName const & f)
118         : cmd(latex), file(f), runparams(rp)
119 {
120         num_errors = 0;
121         if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
122                 depfile = FileName(file.absFilename() + ".dep-pdf");
123                 output_file =
124                         FileName(changeExtension(file.absFilename(), ".pdf"));
125         } else {
126                 depfile = FileName(file.absFilename() + ".dep");
127                 output_file =
128                         FileName(changeExtension(file.absFilename(), ".dvi"));
129         }
130 }
131
132
133 void LaTeX::deleteFilesOnError() const
134 {
135         // currently just a dummy function.
136
137         // What files do we have to delete?
138
139         // This will at least make latex do all the runs
140         depfile.removeFile();
141
142         // but the reason for the error might be in a generated file...
143
144         // bibtex file
145         FileName const bbl(changeExtension(file.absFilename(), ".bbl"));
146         bbl.removeFile();
147
148         // makeindex file
149         FileName const ind(changeExtension(file.absFilename(), ".ind"));
150         ind.removeFile();
151
152         // nomencl file
153         FileName const nls(changeExtension(file.absFilename(), ".nls"));
154         nls.removeFile();
155
156         // nomencl file (old version of the package)
157         FileName const gls(changeExtension(file.absFilename(), ".gls"));
158         gls.removeFile();
159
160         // Also remove the aux file
161         FileName const aux(changeExtension(file.absFilename(), ".aux"));
162         aux.removeFile();
163 }
164
165
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.
172 {
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
179
180         // The class LaTeX does not know the temp path.
181         theBufferList().updateIncludedTeXfiles(getcwd().absFilename(),
182                 runparams);
183
184         // Never write the depfile if an error was encountered.
185
186         // 0
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).
191         //                 -> if changed:
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
197         //             make it and
198         //             run latex once (we need to run latex once anyway) and
199         //             remake the dependency file.
200         //
201
202         bool had_depfile = depfile.exists();
203         bool run_bibtex = false;
204         FileName const aux_file(changeExtension(file.absFilename(), "aux"));
205
206         if (had_depfile) {
207                 LYXERR(Debug::DEPEND, "Dependency file exists");
208                 // Read the dep file:
209                 had_depfile = head.read(depfile);
210         }
211
212         if (had_depfile) {
213                 // Update the checksums
214                 head.update();
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");
224                         return NO_CHANGE;
225                 } else {
226                         LYXERR(Debug::DEPEND, "Dependency file has changed");
227                 }
228
229                 if (head.extchanged(".bib") || head.extchanged(".bst"))
230                         run_bibtex = true;
231         } else
232                 LYXERR(Debug::DEPEND,
233                         "Dependency file does not exist, or has wrong format");
234
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;
239         if (!run_bibtex)
240                 bibtex_info_old = scanAuxFiles(aux_file);
241
242         ++count;
243         LYXERR(Debug::LATEX, "Run #" << count);
244         message(runMessage(count));
245
246         startscript();
247         scanres = scanLogFile(terr);
248         if (scanres & ERROR_RERUN) {
249                 LYXERR(Debug::LATEX, "Rerunning LaTeX");
250                 startscript();
251                 scanres = scanLogFile(terr);
252         }
253
254         if (scanres & ERRORS) {
255                 deleteFilesOnError();
256                 return scanres; // return on error
257         }
258
259         vector<Aux_Info> const bibtex_info = scanAuxFiles(aux_file);
260         if (!run_bibtex && bibtex_info_old != bibtex_info)
261                 run_bibtex = true;
262
263         // update the dependencies.
264         deplog(head); // reads the latex log
265         head.update();
266
267         // 0.5
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
272         // if needed.
273
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();
278
279         // run makeindex
280         if (head.haschanged(idxfile)) {
281                 // no checks for now
282                 LYXERR(Debug::LATEX, "Running MakeIndex.");
283                 message(_("Running MakeIndex."));
284                 // onlyFilename() is needed for cygwin
285                 rerun |= runMakeIndex(onlyFilename(idxfile.absFilename()),
286                                 runparams);
287         }
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");
294
295         // run bibtex
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;
301                 // no checks for now
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);
311         }
312
313         // 1
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
324         //
325         if (rerun || head.sumchange()) {
326                 rerun = false;
327                 ++count;
328                 LYXERR(Debug::DEPEND, "Dep. file has changed or rerun requested");
329                 LYXERR(Debug::LATEX, "Run #" << count);
330                 message(runMessage(count));
331                 startscript();
332                 scanres = scanLogFile(terr);
333                 if (scanres & ERRORS) {
334                         deleteFilesOnError();
335                         return scanres; // return on error
336                 }
337
338                 // update the depedencies
339                 deplog(head); // reads the latex log
340                 head.update();
341         } else {
342                 LYXERR(Debug::DEPEND, "Dep. file has NOT changed");
343         }
344
345         // 1.5
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
351         // more after this.
352
353         // run makeindex if the <file>.idx has changed or was generated.
354         if (head.haschanged(idxfile)) {
355                 // no checks for now
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);
361         }
362
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");
368
369         // 2
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:
373         //             run latex and
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)
378         //
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.
383                 rerun = false;
384                 ++count;
385                 LYXERR(Debug::LATEX, "Run #" << count);
386                 message(runMessage(count));
387                 startscript();
388                 scanres = scanLogFile(terr);
389                 if (scanres & ERRORS) {
390                         deleteFilesOnError();
391                         return scanres; // return on error
392                 }
393
394                 // keep this updated
395                 head.update();
396         }
397
398         // Write the dependencies to file.
399         head.write(depfile);
400         LYXERR(Debug::LATEX, "Done.");
401         return scanres;
402 }
403
404
405 int LaTeX::startscript()
406 {
407         // onlyFilename() is needed for cygwin
408         string tmp = cmd + ' '
409                      + quoteName(onlyFilename(file.toFilesystemEncoding()))
410                      + " > " + os::nulldev();
411         Systemcall one;
412         return one.startscript(Systemcall::Wait, tmp);
413 }
414
415
416 bool LaTeX::runMakeIndex(string const & f, OutputParams const & runparams,
417                          string const & params)
418 {
419         LYXERR(Debug::LATEX,
420                 "idx file has been made, running makeindex on file " << f);
421         string tmp = lyxrc.index_command + ' ';
422
423         tmp = subst(tmp, "$$lang", runparams.document_language);
424         tmp += quoteName(f);
425         tmp += params;
426         Systemcall one;
427         one.startscript(Systemcall::Wait, tmp);
428         return true;
429 }
430
431
432 bool LaTeX::runMakeIndexNomencl(FileName const & file,
433                 OutputParams const & runparams,
434                 string const & nlo, string const & nls)
435 {
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));
441         return runMakeIndex(
442                         onlyFilename(changeExtension(file.absFilename(), nlo)),
443                         runparams, nomenclstr);
444 }
445
446
447 vector<Aux_Info> const
448 LaTeX::scanAuxFiles(FileName const & file)
449 {
450         vector<Aux_Info> result;
451
452         result.push_back(scanAuxFile(file));
453
454         string const basename = removeExtension(file.absFilename());
455         for (int i = 1; i < 1000; ++i) {
456                 FileName const file2(basename
457                         + '.' + convert<string>(i)
458                         + ".aux");
459                 if (!file2.exists())
460                         break;
461                 result.push_back(scanAuxFile(file2));
462         }
463         return result;
464 }
465
466
467 Aux_Info const LaTeX::scanAuxFile(FileName const & file)
468 {
469         Aux_Info result;
470         result.aux_file = file;
471         scanAuxFile(file, result);
472         return result;
473 }
474
475
476 void LaTeX::scanAuxFile(FileName const & file, Aux_Info & aux_info)
477 {
478         LYXERR(Debug::LATEX, "Scanning aux file: " << file);
479
480         ifstream ifs(file.toFilesystemEncoding().c_str());
481         string token;
482         static regex const reg1("\\\\citation\\{([^}]+)\\}");
483         static regex const reg2("\\\\bibdata\\{([^}]+)\\}");
484         static regex const reg3("\\\\bibstyle\\{([^}]+)\\}");
485         static regex const reg4("\\\\@input\\{([^}]+)\\}");
486
487         while (getline(ifs, token)) {
488                 token = rtrim(token, "\r");
489                 smatch sub;
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()) {
496                                 string citation;
497                                 data = split(data, citation, ',');
498                                 LYXERR(Debug::LATEX, "Citation: " << citation);
499                                 aux_info.citations.insert(citation);
500                         }
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()) {
506                                 string database;
507                                 data = split(data, database, ',');
508                                 database = changeExtension(database, "bib");
509                                 LYXERR(Debug::LATEX, "BibTeX database: `" << database << '\'');
510                                 aux_info.databases.insert(database);
511                         }
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);
522                 }
523         }
524 }
525
526
527 void LaTeX::updateBibtexDependencies(DepTable & dep,
528                                      vector<Aux_Info> const & bibtex_info)
529 {
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"));
535
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");
541                         if (!file.empty())
542                                 dep.insert(file, true);
543                 }
544
545                 for (set<string>::const_iterator it2 = it->styles.begin();
546                      it2 != it->styles.end(); ++it2) {
547                         FileName const file = findtexfile(*it2, "bst");
548                         if (!file.empty())
549                                 dep.insert(file, true);
550                 }
551         }
552 }
553
554
555 bool LaTeX::runBibTeX(vector<Aux_Info> const & bibtex_info)
556 {
557         bool result = false;
558         for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
559              it != bibtex_info.end(); ++it) {
560                 if (it->databases.empty())
561                         continue;
562                 result = true;
563
564                 string tmp = lyxrc.bibtex_command + " ";
565                 // onlyFilename() is needed for cygwin
566                 tmp += quoteName(onlyFilename(removeExtension(
567                                 it->aux_file.absFilename())));
568                 Systemcall one;
569                 one.startscript(Systemcall::Wait, tmp);
570         }
571         // Return whether bibtex was run
572         return result;
573 }
574
575
576 int LaTeX::scanLogFile(TeXErrors & terr)
577 {
578         int last_line = -1;
579         int line_count = 1;
580         int retval = NO_ERRORS;
581         string tmp =
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]+: (.+)");
588
589         string token;
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", "");
597                 smatch sub;
598
599                 LYXERR(Debug::LATEX, "Log line: " << token);
600
601                 if (token.empty())
602                         continue;
603
604                 if (contains(token, "file:line:error style messages enabled"))
605                         fle_style = true;
606
607                 if (prefixIs(token, "LaTeX Warning:") ||
608                     prefixIs(token, "! pdfTeX warning")) {
609                         // Here shall we handle different
610                         // types of warnings
611                         retval |= LATEX_WARNING;
612                         LYXERR(Debug::LATEX, "LaTeX Warning.");
613                         if (contains(token, "Rerun to get cross-references")) {
614                                 retval |= RERUN;
615                                 LYXERR(Debug::LATEX, "We should rerun.");
616                         } else if (contains(token, "Citation")
617                                    && contains(token, "on page")
618                                    && contains(token, "undefined")) {
619                                 retval |= UNDEF_CIT;
620                         }
621                 } else if (prefixIs(token, "Package")) {
622                         // Package warnings
623                         retval |= PACKAGE_WARNING;
624                         if (contains(token, "natbib Warning:")) {
625                                 // Natbib warnings
626                                 if (contains(token, "Citation")
627                                     && contains(token, "on page")
628                                     && contains(token, "undefined")) {
629                                         retval |= UNDEF_CIT;
630                                 }
631                         } else if (contains(token, "run BibTeX")) {
632                                 retval |= UNDEF_CIT;
633                         } else if (contains(token, "Rerun LaTeX") ||
634                                    contains(token, "Rerun to get")) {
635                                 // at least longtable.sty and bibtopic.sty
636                                 // might use this.
637                                 LYXERR(Debug::LATEX, "We should rerun.");
638                                 retval |= RERUN;
639                         }
640                 } else if (token[0] == '(') {
641                         if (contains(token, "Rerun LaTeX") ||
642                             contains(token, "Rerun to get")) {
643                                 // Used by natbib
644                                 LYXERR(Debug::LATEX, "We should rerun.");
645                                 retval |= RERUN;
646                         }
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.
651
652                         // Just get the error description:
653                         string desc;
654                         if (prefixIs(token, "! "))
655                                 desc = string(token, 2);
656                         else if (fle_style)
657                                 desc = sub.str();
658                         if (contains(token, "LaTeX Error:"))
659                                 retval |= LATEX_ERROR;
660                         // get the next line
661                         string tmp;
662                         int count = 0;
663                         do {
664                                 if (!getline(ifs, tmp))
665                                         break;
666                                 if (++count > 10)
667                                         break;
668                         } while (!prefixIs(tmp, "l."));
669                         if (prefixIs(tmp, "l.")) {
670                                 // we have a latex error
671                                 retval |=  TEX_ERROR;
672                                 if (contains(desc,
673                                     "Package babel Error: You haven't defined the language") ||
674                                     contains(desc,
675                                     "Package babel Error: You haven't loaded the option"))
676                                         retval |= ERROR_RERUN;
677                                 // get the line number:
678                                 int line = 0;
679                                 sscanf(tmp.c_str(), "l.%d", &line);
680                                 // get the rest of the message:
681                                 string errstr(tmp, tmp.find(' '));
682                                 errstr += '\n';
683                                 getline(ifs, tmp);
684                                 while (!contains(errstr, "l.")
685                                        && !tmp.empty()
686                                        && !prefixIs(tmp, "! ")
687                                        && !contains(tmp, "(job aborted")) {
688                                         errstr += tmp;
689                                         errstr += "\n";
690                                         getline(ifs, tmp);
691                                 }
692                                 LYXERR(Debug::LATEX, "line: " << line << '\n'
693                                         << "Desc: " << desc << '\n' << "Text: " << errstr);
694                                 if (line == last_line)
695                                         ++line_count;
696                                 else {
697                                         line_count = 1;
698                                         last_line = line;
699                                 }
700                                 if (line_count <= 5) {
701                                         // FIXME UNICODE
702                                         // We have no idea what the encoding of
703                                         // the log file is.
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));
712                                         ++num_errors;
713                                 }
714                         }
715                 } else {
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.
724                                 retval |= UNDEF_CIT;
725                         } else if (contains(token, "No pages of output")) {
726                                 // A dvi file was not created
727                                 retval |= NO_OUTPUT;
728                         } else if (contains(token, "That makes 100 errors")) {
729                                 // More than 100 errors were reprted
730                                 retval |= TOO_MANY_ERRORS;
731                         }
732                 }
733         }
734         LYXERR(Debug::LATEX, "Log line: " << token);
735         return retval;
736 }
737
738
739 namespace {
740
741 bool insertIfExists(FileName const & absname, DepTable & head)
742 {
743         if (absname.exists() && !absname.isDirectory()) {
744                 head.insert(absname, true);
745                 return true;
746         }
747         return false;
748 }
749
750
751 bool handleFoundFile(string const & ff, DepTable & head)
752 {
753         // convert from native os path to unix path
754         string foundfile = os::internal_path(trim(ff));
755
756         LYXERR(Debug::DEPEND, "Found file: " << foundfile);
757
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
762         // find the file.
763
764         // (1) foundfile is an
765         //     absolute path and should
766         //     be inserted.
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
771                 // the latex run.
772                 FileName absname(foundfile);
773                 if (!insertIfExists(absname, head)) {
774                         // check for spaces
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))
782                                         return true;
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))
789                                         return true;
790                         }
791                 }
792         }
793
794         string onlyfile = onlyFilename(foundfile);
795         FileName absname(makeAbsPath(onlyfile));
796
797         // check for spaces
798         while (contains(foundfile, ' ')) {
799                 if (absname.exists())
800                         // everything o.k.
801                         break;
802                 else {
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())
808                                 break;
809                         // strip off part after last space and try again
810                         string strippedfile;
811                         string const stripoff =
812                                 rsplit(foundfile, strippedfile, ' ');
813                         foundfile = strippedfile;
814                         onlyfile = onlyFilename(strippedfile);
815                         absname = makeAbsPath(onlyfile);
816                 }
817         }
818
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
831                         // during its runs.
832                         LYXERR(Debug::DEPEND, "Tmpdir TeX file: " << onlyfile);
833                         head.insert(absname, true);
834                 } else {
835                         LYXERR(Debug::DEPEND, "In tmpdir file:" << onlyfile);
836                         head.insert(absname);
837                 }
838                 return true;
839         } else {
840                 LYXERR(Debug::DEPEND, "Not a file or we are unable to find it.");
841                 return false;
842         }
843 }
844
845
846 bool checkLineBreak(string const & ff, DepTable & head)
847 {
848         if (!contains(ff, '.'))
849                 return false;
850
851         // if we have a dot, we let handleFoundFile decide
852         return handleFoundFile(ff, head);
853 }
854
855 } // anon namespace
856
857
858 void LaTeX::deplog(DepTable & head)
859 {
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.
863
864         string const logfile =
865                 onlyFilename(changeExtension(file.absFilename(), ".log"));
866
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:
882         //    \tf@toc=\write#
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(".*\\([^)]+.*");
887
888         FileName const fn = makeAbsPath(logfile);
889         ifstream ifs(fn.toFilesystemEncoding().c_str());
890         string lastline;
891         while (ifs) {
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;
897                 string token;
898                 getline(ifs, token);
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 == ")") {
906                         lastline = string();
907                         continue;
908                 }
909
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))
926                                 lastline = string();
927                 }
928
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);
935                 }
936
937                 smatch sub;
938
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));
942
943                 // (1) "File: file.ext"
944                 if (regex_match(token, sub, reg1)) {
945                         // check for dot
946                         found_file = checkLineBreak(sub.str(1), head);
947                         // However, ...
948                         if (suffixIs(token, ")"))
949                                 // no line break for sure
950                                 // pretend we've been succesfully searching
951                                 found_file = true;
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);
957                         else
958                                 // we suspect a line break
959                                 found_file = false;
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);
965                         else
966                                 // probable line break
967                                 found_file = false;
968                 // (4) "Writing index file file.ext"
969                 } else if (regex_match(token, sub, reg4))
970                         // check for dot
971                         found_file = checkLineBreak(sub.str(1), head);
972                 // (5) "<file.ext>"
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);
977                         else
978                                 // probable line break
979                                 found_file = false;
980                 // (6) "Writing nomenclature file file.ext"
981                 } else if (regex_match(token, sub, regnomencl) ||
982                            regex_match(token, sub, regoldnomencl))
983                         // check for dot
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);
989                 else
990                         // not found, but we won't check further
991                         // pretend we've been succesfully searching
992                         found_file = true;
993
994                 // (8) "(file.ext"
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("\\(([^()]+)(.).*");
1002                         smatch what;
1003                         string::const_iterator first = token.begin();
1004                         string::const_iterator end = token.end();
1005
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
1014                                                 found_file = true;
1015                                         } else
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);
1020                                 }
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;
1025                                         found_file = false;
1026                                 } else {
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;
1032                                         found_file = true;
1033                                 }
1034                         }
1035                 }
1036
1037                 if (!found_file)
1038                         // probable linebreak:
1039                         // save this line
1040                         lastline = token;
1041                 else
1042                         // no linebreak: reset
1043                         lastline = string();
1044         }
1045
1046         // Make sure that the main .tex file is in the dependency file.
1047         head.insert(file, true);
1048 }
1049
1050
1051 } // namespace lyx