]> git.lyx.org Git - lyx.git/blob - src/LaTeX.C
0de75eb78641c1f99f2e7b6bff7a4f92ef8d81bc
[lyx.git] / src / LaTeX.C
1 /**
2  * \file LaTeX.C
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  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "LaTeX.h"
18 #include "bufferlist.h"
19 #include "gettext.h"
20 #include "debug.h"
21 #include "DepTable.h"
22 #include "support/filetools.h"
23 #include "support/FileInfo.h"
24 #include "support/tostr.h"
25 #include "support/lstrings.h"
26 #include "support/lyxlib.h"
27 #include "support/systemcall.h"
28 #include "support/os.h"
29
30 #include <boost/regex.hpp>
31
32 #include <fstream>
33
34 using lyx::support::AbsolutePath;
35 using lyx::support::bformat;
36 using lyx::support::ChangeExtension;
37 using lyx::support::contains;
38 using lyx::support::FileInfo;
39 using lyx::support::findtexfile;
40 using lyx::support::getcwd;
41 using lyx::support::OnlyFilename;
42 using lyx::support::os;
43 using lyx::support::prefixIs;
44 using lyx::support::QuoteName;
45 using lyx::support::rtrim;
46 using lyx::support::split;
47 using lyx::support::suffixIs;
48 using lyx::support::Systemcall;
49 using lyx::support::unlink;
50
51 #ifndef CXX_GLOBAL_CSTD
52 using std::sscanf;
53 #endif
54
55 using std::endl;
56 using std::getline;
57
58 using std::ifstream;
59 using std::set;
60 using std::vector;
61
62 using boost::regex;
63
64 #ifndef USE_INCLUDED_STRING
65 using boost::smatch;
66 #else
67 using boost::cmatch;
68 #endif
69
70
71 // TODO: in no particular order
72 // - get rid of the extern BufferList and the call to
73 //   BufferList::updateIncludedTeXfiles, this should either
74 //   be done before calling LaTeX::funcs or in a completely
75 //   different way.
76 // - the bibtex command options should be supported.
77 // - the makeindex style files should be taken care of with
78 //   the dependency mechanism.
79 // - makeindex commandline options should be supported
80 // - somewhere support viewing of bibtex and makeindex log files.
81 // - we should perhaps also scan the bibtex log file
82 // - we should perhaps also scan the bibtex log file
83
84 extern BufferList bufferlist;
85
86 namespace {
87
88 string runMessage(unsigned int count)
89 {
90         return bformat(_("Waiting for LaTeX run number %1$s"), tostr(count));
91 }
92
93 };
94
95 /*
96  * CLASS TEXERRORS
97  */
98
99 void TeXErrors::insertError(int line, string const & error_desc,
100                             string const & error_text)
101 {
102         Error newerr(line, error_desc, error_text);
103         errors.push_back(newerr);
104 }
105
106
107 bool operator==(Aux_Info const & a, Aux_Info const & o)
108 {
109         return a.aux_file == o.aux_file &&
110                 a.citations == o.citations &&
111                 a.databases == o.databases &&
112                 a.styles == o.styles;
113 }
114
115
116 bool operator!=(Aux_Info const & a, Aux_Info const & o)
117 {
118         return !(a == o);
119 }
120
121
122 /*
123  * CLASS LaTeX
124  */
125
126 LaTeX::LaTeX(string const & latex, LatexRunParams const & rp,
127              string const & f, string const & p)
128         : cmd(latex), file(f), path(p), runparams(rp)
129 {
130         num_errors = 0;
131         depfile = file + ".dep";
132         if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
133                 depfile += "-pdf";
134                 output_file = ChangeExtension(file,".pdf");
135         } else {
136                 output_file = ChangeExtension(file,".dvi");
137         }
138 }
139
140
141 void LaTeX::deleteFilesOnError() const
142 {
143         // currently just a dummy function.
144
145         // What files do we have to delete?
146
147         // This will at least make latex do all the runs
148         unlink(depfile);
149
150         // but the reason for the error might be in a generated file...
151
152         string const ofname = OnlyFilename(file);
153
154         // bibtex file
155         string const bbl = ChangeExtension(ofname, ".bbl");
156         unlink(bbl);
157
158         // makeindex file
159         string const ind = ChangeExtension(ofname, ".ind");
160         unlink(ind);
161
162         // Also remove the aux file
163         string const aux = ChangeExtension(ofname, ".aux");
164         unlink(aux);
165 }
166
167
168 int LaTeX::run(TeXErrors & terr)
169         // We know that this function will only be run if the lyx buffer
170         // has been changed. We also know that a newly written .tex file
171         // is always different from the previous one because of the date
172         // in it. However it seems safe to run latex (at least) on time each
173         // time the .tex file changes.
174 {
175         int scanres = NO_ERRORS;
176         unsigned int count = 0; // number of times run
177         num_errors = 0; // just to make sure.
178         unsigned int const MAX_RUN = 6;
179         DepTable head; // empty head
180         bool rerun = false; // rerun requested
181
182         // The class LaTeX does not know the temp path.
183         bufferlist.updateIncludedTeXfiles(getcwd(), runparams);
184
185         // Never write the depfile if an error was encountered.
186
187         // 0
188         // first check if the file dependencies exist:
189         //     ->If it does exist
190         //             check if any of the files mentioned in it have
191         //             changed (done using a checksum).
192         //                 -> if changed:
193         //                        run latex once and
194         //                        remake the dependency file
195         //                 -> if not changed:
196         //                        just return there is nothing to do for us.
197         //     ->if it doesn't exist
198         //             make it and
199         //             run latex once (we need to run latex once anyway) and
200         //             remake the dependency file.
201         //
202
203         FileInfo fi(depfile);
204         bool had_depfile = fi.exist();
205         bool run_bibtex = false;
206         string aux_file = OnlyFilename(ChangeExtension(file, "aux"));
207
208         if (had_depfile) {
209                 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
210                 // Read the dep file:
211                 had_depfile = head.read(depfile);
212         }
213
214         if (had_depfile) {
215                 // Update the checksums
216                 head.update();
217                 // Can't just check if anything has changed because it might have aborted
218                 // on error last time... in which cas we need to re-run latex
219                 // and collect the error messages (even if they are the same).
220                 if (!FileInfo(output_file).exist()) {
221                         lyxerr[Debug::DEPEND]
222                                 << "re-running LaTeX because output file doesn't exist." << endl;
223                 } else if (!head.sumchange()) {
224                         lyxerr[Debug::DEPEND] << "return no_change" << endl;
225                         return NO_CHANGE;
226                 } else {
227                         lyxerr[Debug::DEPEND]
228                                 << "Dependency file has changed" << endl;
229                 }
230
231                 if (head.extchanged(".bib") || head.extchanged(".bst"))
232                         run_bibtex = true;
233         } else
234                 lyxerr[Debug::DEPEND]
235                         << "Dependency file does not exist, or has wrong format" << endl;
236
237         /// We scan the aux file even when had_depfile = false,
238         /// because we can run pdflatex on the file after running latex on it,
239         /// in which case we will not need to run bibtex again.
240         vector<Aux_Info> bibtex_info_old;
241         if (!run_bibtex)
242                 bibtex_info_old = scanAuxFiles(aux_file);
243
244         ++count;
245         lyxerr[Debug::LATEX] << "Run #" << count << endl;
246         message(runMessage(count));
247
248         startscript();
249         scanres = scanLogFile(terr);
250         if (scanres & ERROR_RERUN) {
251                 lyxerr[Debug::LATEX] << "Rerunning LaTeX" << endl;
252                 startscript();
253                 scanres = scanLogFile(terr);
254         }
255
256         if (scanres & ERRORS) {
257                 deleteFilesOnError();
258                 return scanres; // return on error
259         }
260
261         vector<Aux_Info> const bibtex_info = scanAuxFiles(aux_file);
262         if (!run_bibtex && bibtex_info_old != bibtex_info)
263                 run_bibtex = true;
264
265         // update the dependencies.
266         deplog(head); // reads the latex log
267         head.update();
268
269         // 0.5
270         // At this point we must run external programs if needed.
271         // makeindex will be run if a .idx file changed or was generated.
272         // And if there were undefined citations or changes in references
273         // the .aux file is checked for signs of bibtex. Bibtex is then run
274         // if needed.
275
276         // run makeindex
277         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
278                 // no checks for now
279                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
280                 message(_("Running MakeIndex."));
281                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
282         }
283
284         // run bibtex
285         // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
286         if (scanres & UNDEF_CIT || run_bibtex) {
287                 // Here we must scan the .aux file and look for
288                 // "\bibdata" and/or "\bibstyle". If one of those
289                 // tags is found -> run bibtex and set rerun = true;
290                 // no checks for now
291                 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
292                 message(_("Running BibTeX."));
293                 updateBibtexDependencies(head, bibtex_info);
294                 rerun |= runBibTeX(bibtex_info);
295         } else if (!had_depfile) {
296                 /// If we run pdflatex on the file after running latex on it,
297                 /// then we do not need to run bibtex, but we do need to
298                 /// insert the .bib and .bst files into the .dep-pdf file.
299                 updateBibtexDependencies(head, bibtex_info);
300         }
301
302         // 1
303         // we know on this point that latex has been run once (or we just
304         // returned) and the question now is to decide if we need to run
305         // it any more. This is done by asking if any of the files in the
306         // dependency file has changed. (remember that the checksum for
307         // a given file is reported to have changed if it just was created)
308         //     -> if changed or rerun == true:
309         //             run latex once more and
310         //             update the dependency structure
311         //     -> if not changed:
312         //             we does nothing at this point
313         //
314         if (rerun || head.sumchange()) {
315                 rerun = false;
316                 ++count;
317                 lyxerr[Debug::DEPEND]
318                         << "Dep. file has changed or rerun requested" << endl;
319                 lyxerr[Debug::LATEX]
320                         << "Run #" << count << endl;
321                 message(runMessage(count));
322                 startscript();
323                 scanres = scanLogFile(terr);
324                 if (scanres & ERRORS) {
325                         deleteFilesOnError();
326                         return scanres; // return on error
327                 }
328
329                 // update the depedencies
330                 deplog(head); // reads the latex log
331                 head.update();
332         } else {
333                 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
334         }
335
336         // 1.5
337         // The inclusion of files generated by external programs like
338         // makeindex or bibtex might have done changes to pagenumbereing,
339         // etc. And because of this we must run the external programs
340         // again to make sure everything is redone correctly.
341         // Also there should be no need to run the external programs any
342         // more after this.
343
344         // run makeindex if the <file>.idx has changed or was generated.
345         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
346                 // no checks for now
347                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
348                 message(_("Running MakeIndex."));
349                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
350         }
351
352         // 2
353         // we will only run latex more if the log file asks for it.
354         // or if the sumchange() is true.
355         //     -> rerun asked for:
356         //             run latex and
357         //             remake the dependency file
358         //             goto 2 or return if max runs are reached.
359         //     -> rerun not asked for:
360         //             just return (fall out of bottom of func)
361         //
362         while ((head.sumchange() || rerun || (scanres & RERUN))
363                && count < MAX_RUN) {
364                 // Yes rerun until message goes away, or until
365                 // MAX_RUNS are reached.
366                 rerun = false;
367                 ++count;
368                 lyxerr[Debug::LATEX] << "Run #" << count << endl;
369                 message(runMessage(count));
370                 startscript();
371                 scanres = scanLogFile(terr);
372                 if (scanres & ERRORS) {
373                         deleteFilesOnError();
374                         return scanres; // return on error
375                 }
376
377                 // keep this updated
378                 head.update();
379         }
380
381         // Write the dependencies to file.
382         head.write(depfile);
383         lyxerr[Debug::LATEX] << "Done." << endl;
384         return scanres;
385 }
386
387
388 int LaTeX::startscript()
389 {
390 #ifndef __EMX__
391         string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
392 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
393         string tmp = cmd + ' ' + file + " > nul";
394 #endif
395         Systemcall one;
396         return one.startscript(Systemcall::Wait, tmp);
397 }
398
399
400 bool LaTeX::runMakeIndex(string const & f)
401 {
402         lyxerr[Debug::LATEX] << "idx file has been made,"
403                 " running makeindex on file "
404                              <<  f << endl;
405
406         // It should be possible to set the switches for makeindex
407         // sorting style and such. It would also be very convenient
408         // to be able to make style files from within LyX. This has
409         // to come for a later time.
410         string tmp = "makeindex -c -q ";
411         tmp += f;
412         Systemcall one;
413         one.startscript(Systemcall::Wait, tmp);
414         return true;
415 }
416
417
418 vector<Aux_Info> const
419 LaTeX::scanAuxFiles(string const & file)
420 {
421         vector<Aux_Info> result;
422
423         result.push_back(scanAuxFile(file));
424
425         for (int i = 1; i < 1000; ++i) {
426                 string file2 = ChangeExtension(file, "") + '.' + tostr(i)
427                         + ".aux";
428                 FileInfo fi(file2);
429                 if (!fi.exist())
430                         break;
431                 result.push_back(scanAuxFile(file2));
432         }
433         return result;
434 }
435
436
437 Aux_Info const LaTeX::scanAuxFile(string const & file)
438 {
439         Aux_Info result;
440         result.aux_file = file;
441         scanAuxFile(file, result);
442         return result;
443 }
444
445
446 void LaTeX::scanAuxFile(string const & file, Aux_Info & aux_info)
447 {
448         lyxerr[Debug::LATEX] << "Scanning aux file: " << file << endl;
449
450         ifstream ifs(file.c_str());
451         string token;
452         regex reg1("\\\\citation\\{([^}]+)\\}");
453         regex reg2("\\\\bibdata\\{([^}]+)\\}");
454         regex reg3("\\\\bibstyle\\{([^}]+)\\}");
455         regex reg4("\\\\@input\\{([^}]+)\\}");
456
457         while (getline(ifs, token)) {
458                 token = rtrim(token, "\r");
459 #ifndef USE_INCLUDED_STRING
460                 smatch sub;
461 #else
462                 cmatch sub;
463 #endif
464                 if (regex_match(STRCONV(token), sub, reg1)) {
465                         string data = STRCONV(sub.str(1));
466                         while (!data.empty()) {
467                                 string citation;
468                                 data = split(data, citation, ',');
469                                 lyxerr[Debug::LATEX] << "Citation: "
470                                                      << citation << endl;
471                                 aux_info.citations.insert(citation);
472                         }
473                 } else if (regex_match(STRCONV(token), sub, reg2)) {
474                         string data = sub.STRCONV(str(1));
475                         // data is now all the bib files separated by ','
476                         // get them one by one and pass them to the helper
477                         while (!data.empty()) {
478                                 string database;
479                                 data = split(data, database, ',');
480                                 database = ChangeExtension(database, "bib");
481                                 lyxerr[Debug::LATEX] << "BibTeX database: `"
482                                                      << database << '\'' << endl;
483                                 aux_info.databases.insert(database);
484                         }
485                 } else if (regex_match(STRCONV(token), sub, reg3)) {
486                         string style = STRCONV(sub.str(1));
487                         // token is now the style file
488                         // pass it to the helper
489                         style = ChangeExtension(style, "bst");
490                         lyxerr[Debug::LATEX] << "BibTeX style: `"
491                                              << style << '\'' << endl;
492                         aux_info.styles.insert(style);
493                 } else if (regex_match(STRCONV(token), sub, reg4)) {
494                         string const file2 = STRCONV(sub.str(1));
495                         scanAuxFile(file2, aux_info);
496                 }
497         }
498 }
499
500
501 void LaTeX::updateBibtexDependencies(DepTable & dep,
502                                      vector<Aux_Info> const & bibtex_info)
503 {
504         // Since a run of Bibtex mandates more latex runs it is ok to
505         // remove all ".bib" and ".bst" files.
506         dep.remove_files_with_extension(".bib");
507         dep.remove_files_with_extension(".bst");
508         //string aux = OnlyFilename(ChangeExtension(file, ".aux"));
509
510         for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
511              it != bibtex_info.end(); ++it) {
512                 for (set<string>::const_iterator it2 = it->databases.begin();
513                      it2 != it->databases.end(); ++it2) {
514                         string file = findtexfile(*it2, "bib");
515                         if (!file.empty())
516                                 dep.insert(file, true);
517                 }
518
519                 for (set<string>::const_iterator it2 = it->styles.begin();
520                      it2 != it->styles.end(); ++it2) {
521                         string file = findtexfile(*it2, "bst");
522                         if (!file.empty())
523                                 dep.insert(file, true);
524                 }
525         }
526 }
527
528
529 bool LaTeX::runBibTeX(vector<Aux_Info> const & bibtex_info)
530 {
531         bool result = false;
532         for (vector<Aux_Info>::const_iterator it = bibtex_info.begin();
533              it != bibtex_info.end(); ++it) {
534                 if (it->databases.empty())
535                         continue;
536                 result = true;
537
538                 string tmp = "bibtex ";
539                 tmp += OnlyFilename(ChangeExtension(it->aux_file, string()));
540                 Systemcall one;
541                 one.startscript(Systemcall::Wait, tmp);
542         }
543         // Return whether bibtex was run
544         return result;
545 }
546
547
548 int LaTeX::scanLogFile(TeXErrors & terr)
549 {
550         int last_line = -1;
551         int line_count = 1;
552         int retval = NO_ERRORS;
553         string tmp = OnlyFilename(ChangeExtension(file, ".log"));
554         lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
555         ifstream ifs(tmp.c_str());
556
557         string token;
558         while (getline(ifs, token)) {
559                 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
560
561                 if (token.empty())
562                         continue;
563
564                 if (prefixIs(token, "LaTeX Warning:")) {
565                         // Here shall we handle different
566                         // types of warnings
567                         retval |= LATEX_WARNING;
568                         lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
569                         if (contains(token, "Rerun to get cross-references")) {
570                                 retval |= RERUN;
571                                 lyxerr[Debug::LATEX]
572                                         << "We should rerun." << endl;
573                         } else if (contains(token, "Citation")
574                                    && contains(token, "on page")
575                                    && contains(token, "undefined")) {
576                                 retval |= UNDEF_CIT;
577                         }
578                 } else if (prefixIs(token, "Package")) {
579                         // Package warnings
580                         retval |= PACKAGE_WARNING;
581                         if (contains(token, "natbib Warning:")) {
582                                 // Natbib warnings
583                                 if (contains(token, "Citation")
584                                     && contains(token, "on page")
585                                     && contains(token, "undefined")) {
586                                         retval |= UNDEF_CIT;
587                                 }
588                         } else if (contains(token, "run BibTeX")) {
589                                 retval |= UNDEF_CIT;
590                         } else if (contains(token, "Rerun LaTeX") ||
591                                    contains(token, "Rerun to get")) {
592                                 // at least longtable.sty and bibtopic.sty
593                                 // might use this.
594                                 lyxerr[Debug::LATEX]
595                                         << "We should rerun." << endl;
596                                 retval |= RERUN;
597                         }
598                 } else if (token[0] == '(') {
599                         if (contains(token, "Rerun LaTeX") ||
600                             contains(token, "Rerun to get")) {
601                                 // Used by natbib
602                                 lyxerr[Debug::LATEX]
603                                         << "We should rerun." << endl;
604                                 retval |= RERUN;
605                         }
606                 } else if (prefixIs(token, "! ")) {
607                         // Ok, we have something that looks like a TeX Error
608                         // but what do we really have.
609
610                         // Just get the error description:
611                         string desc(token, 2);
612                         if (contains(token, "LaTeX Error:"))
613                                 retval |= LATEX_ERROR;
614                         // get the next line
615                         string tmp;
616                         int count = 0;
617                         do {
618                                 if (!getline(ifs, tmp))
619                                         break;
620                                 if (++count > 10)
621                                         break;
622                         } while (!prefixIs(tmp, "l."));
623                         if (prefixIs(tmp, "l.")) {
624                                 // we have a latex error
625                                 retval |=  TEX_ERROR;
626                                 if (contains(desc, "Package babel Error: You haven't defined the language"))
627                                         retval |= ERROR_RERUN;
628                                 // get the line number:
629                                 int line = 0;
630                                 sscanf(tmp.c_str(), "l.%d", &line);
631                                 // get the rest of the message:
632                                 string errstr(tmp, tmp.find(' '));
633                                 errstr += '\n';
634                                 getline(ifs, tmp);
635                                 while (!contains(errstr, "l.")
636                                        && !tmp.empty()
637                                        && !prefixIs(tmp, "! ")
638                                        && !contains(tmp, "(job aborted")) {
639                                         errstr += tmp;
640                                         errstr += "\n";
641                                         getline(ifs, tmp);
642                                 }
643                                 lyxerr[Debug::LATEX]
644                                         << "line: " << line << '\n'
645                                         << "Desc: " << desc << '\n'
646                                         << "Text: " << errstr << endl;
647                                 if (line == last_line)
648                                         ++line_count;
649                                 else {
650                                         line_count = 1;
651                                         last_line = line;
652                                 }
653                                 if (line_count <= 5) {
654                                         terr.insertError(line, desc, errstr);
655                                         ++num_errors;
656                                 }
657                         }
658                 } else {
659                         // information messages, TeX warnings and other
660                         // warnings we have not caught earlier.
661                         if (prefixIs(token, "Overfull ")) {
662                                 retval |= TEX_WARNING;
663                         } else if (prefixIs(token, "Underfull ")) {
664                                 retval |= TEX_WARNING;
665                         } else if (contains(token, "Rerun to get citations")) {
666                                 // Natbib seems to use this.
667                                 retval |= UNDEF_CIT;
668                         } else if (contains(token, "No pages of output")) {
669                                 // A dvi file was not created
670                                 retval |= NO_OUTPUT;
671                         } else if (contains(token, "That makes 100 errors")) {
672                                 // More than 100 errors were reprted
673                                 retval |= TOO_MANY_ERRORS;
674                         }
675                 }
676         }
677         lyxerr[Debug::LATEX] << "Log line: " << token << endl;
678         return retval;
679 }
680
681
682 void LaTeX::deplog(DepTable & head)
683 {
684         // This function reads the LaTeX log file end extracts all the external
685         // files used by the LaTeX run. The files are then entered into the
686         // dependency file.
687
688         string const logfile = OnlyFilename(ChangeExtension(file, ".log"));
689
690         regex reg1("\\)* *\\(([^ )]+).*");
691         regex reg2("File: ([^ ]+).*");
692         regex reg3("No file ([^ ]+)\\..*");
693         regex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
694         // If an index should be created, MikTex does not write a line like
695         //    \openout# = 'sample,idx'.
696         // but intstead only a line like this into the log:
697         //   Writing index file sample.idx
698         regex reg5("Writing index file ([^ ]+).*");
699         regex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
700
701         ifstream ifs(logfile.c_str());
702         while (ifs) {
703                 // Ok, the scanning of files here is not sufficient.
704                 // Sometimes files are named by "File: xxx" only
705                 // So I think we should use some regexps to find files instead.
706                 // "(\([^ ]+\)"   should match the "(file " variant
707                 // "File: \([^ ]+\)" should match the "File: file" variant
708                 string foundfile;
709                 string token;
710                 getline(ifs, token);
711                 token = rtrim(token, "\r");
712                 if (token.empty()) continue;
713
714 #ifndef USE_INCLUDED_STRING
715                 smatch sub;
716 #else
717                 cmatch sub;
718 #endif
719                 if (regex_match(STRCONV(token), sub, reg1)) {
720                         foundfile = STRCONV(sub.str(1));
721                 } else if (regex_match(STRCONV(token), sub, reg2)) {
722                         foundfile = STRCONV(sub.str(1));
723                 } else if (regex_match(STRCONV(token), sub, reg3)) {
724                         foundfile = STRCONV(sub.str(1));
725                 } else if (regex_match(STRCONV(token), sub, reg4)) {
726                         foundfile = STRCONV(sub.str(1));
727                 } else if (regex_match(STRCONV(token), sub, reg5)) {
728                         foundfile = STRCONV(sub.str(1));
729                 } else {
730                         continue;
731                 }
732
733                 // convert from native os path to unix path
734                 foundfile = os::internal_path(foundfile);
735
736                 lyxerr[Debug::DEPEND] << "Found file: "
737                                       << foundfile << endl;
738
739                 // Ok now we found a file.
740                 // Now we should make sure that this is a file that we can
741                 // access through the normal paths.
742                 // We will not try any fancy search methods to
743                 // find the file.
744
745                 // (1) foundfile is an
746                 //     absolute path and should
747                 //     be inserted.
748                 if (AbsolutePath(foundfile)) {
749                         lyxerr[Debug::DEPEND] << "AbsolutePath file: "
750                                               << foundfile << endl;
751                         // On initial insert we want to do the update at once
752                         // since this file can not be a file generated by
753                         // the latex run.
754                         if (FileInfo(foundfile).exist())
755                                 head.insert(foundfile, true);
756                 }
757
758                 // (2) foundfile is in the tmpdir
759                 //     insert it into head
760                 else if (FileInfo(OnlyFilename(foundfile)).exist()) {
761                         if (regex_match(STRCONV(foundfile), unwanted)) {
762                                 lyxerr[Debug::DEPEND]
763                                         << "We don't want "
764                                         << OnlyFilename(foundfile)
765                                         << " in the dep file"
766                                         << endl;
767                         } else if (suffixIs(foundfile, ".tex")) {
768                                 // This is a tex file generated by LyX
769                                 // and latex is not likely to change this
770                                 // during its runs.
771                                 lyxerr[Debug::DEPEND]
772                                         << "Tmpdir TeX file: "
773                                         << OnlyFilename(foundfile)
774                                         << endl;
775                                 head.insert(foundfile, true);
776                         } else {
777                                 lyxerr[Debug::DEPEND]
778                                         << "In tmpdir file:"
779                                         << OnlyFilename(foundfile)
780                                         << endl;
781                                 head.insert(OnlyFilename(foundfile));
782                         }
783                 } else
784                         lyxerr[Debug::DEPEND]
785                                 << "Not a file or we are unable to find it."
786                                 << endl;
787         }
788
789         // Make sure that the main .tex file is in the dependancy file.
790         head.insert(OnlyFilename(file), true);
791 }