]> git.lyx.org Git - lyx.git/blob - src/LaTeX.C
non-templated tostr in separate files
[lyx.git] / src / LaTeX.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  *           This file is Copyright 1996-2001
9  *           Lars Gullik Bjønnes
10  *
11  * ======================================================
12  */
13
14 #include <config.h>
15
16 #include "LaTeX.h"
17 #include "bufferlist.h"
18 #include "gettext.h"
19 #include "lyxfunc.h"
20 #include "debug.h"
21 #include "funcrequest.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 #include "support/path.h"
30
31 #include <boost/regex.hpp>
32
33 #include <fstream>
34 #include <cstdio>  // sscanf
35
36 #ifndef CXX_GLOBAL_CSTD
37 using std::sscanf;
38 #endif
39
40 using std::ifstream;
41 using std::getline;
42 using std::endl;
43 using std::vector;
44 using std::set;
45 using boost::regex;
46 using boost::regex_match;
47
48 #ifndef USE_INCLUDED_STRING
49 using boost::smatch;
50 #else
51 using boost::cmatch;
52 #endif
53
54 // TODO: in no particular order
55 // - get rid of the extern BufferList and the call to
56 //   BufferList::updateIncludedTeXfiles, this should either
57 //   be done before calling LaTeX::funcs or in a completely
58 //   different way.
59 // - the bibtex command options should be supported.
60 // - the makeindex style files should be taken care of with
61 //   the dependency mechanism.
62 // - makeindex commandline options should be supported
63 // - somewhere support viewing of bibtex and makeindex log files.
64 // - we should perhaps also scan the bibtex log file
65 // - we should perhaps also scan the bibtex log file
66
67 extern BufferList bufferlist;
68
69 namespace {
70
71 void showRunMessage(LyXFunc * lf, unsigned int count)
72 {
73         string str = bformat(_("Waiting for LaTeX run number %1$s"), tostr(count));
74         lf->dispatch(FuncRequest(LFUN_MESSAGE, str));
75 }
76
77
78 };
79 /*
80  * CLASS TEXERRORS
81  */
82
83 void TeXErrors::insertError(int line, string const & error_desc,
84                             string const & error_text)
85 {
86         Error newerr(line, error_desc, error_text);
87         errors.push_back(newerr);
88 }
89
90
91 bool operator==(Aux_Info const & a, Aux_Info const & o)
92 {
93         return a.aux_file == o.aux_file &&
94                 a.citations == o.citations &&
95                 a.databases == o.databases &&
96                 a.styles == o.styles;
97 }
98
99
100 bool operator!=(Aux_Info const & a, Aux_Info const & o)
101 {
102         return !(a == o);
103 }
104
105
106 /*
107  * CLASS LaTeX
108  */
109
110 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
111                 : cmd(latex), file(f), path(p)
112 {
113         num_errors = 0;
114         depfile = file + ".dep";
115         if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
116                 depfile += "-pdf";
117                 output_file = ChangeExtension(file,".pdf");
118         } else {
119                 output_file = ChangeExtension(file,".dvi");
120         }
121 }
122
123
124 void LaTeX::deleteFilesOnError() const
125 {
126         // currently just a dummy function.
127
128         // What files do we have to delete?
129
130         // This will at least make latex do all the runs
131         lyx::unlink(depfile);
132
133         // but the reason for the error might be in a generated file...
134
135         string const ofname = OnlyFilename(file);
136
137         // bibtex file
138         string const bbl = ChangeExtension(ofname, ".bbl");
139         lyx::unlink(bbl);
140
141         // makeindex file
142         string const ind = ChangeExtension(ofname, ".ind");
143         lyx::unlink(ind);
144
145         // Also remove the aux file
146         string const aux = ChangeExtension(ofname, ".aux");
147         lyx::unlink(aux);
148 }
149
150
151 int LaTeX::run(TeXErrors & terr, LyXFunc * lfun)
152         // We know that this function will only be run if the lyx buffer
153         // has been changed. We also know that a newly written .tex file
154         // is always different from the previous one because of the date
155         // in it. However it seems safe to run latex (at least) on time each
156         // time the .tex file changes.
157 {
158         int scanres = NO_ERRORS;
159         unsigned int count = 0; // number of times run
160         num_errors = 0; // just to make sure.
161         unsigned int const MAX_RUN = 6;
162         DepTable head; // empty head
163         bool rerun = false; // rerun requested
164
165         // The class LaTeX does not know the temp path.
166         bufferlist.updateIncludedTeXfiles(lyx::getcwd());
167
168         // Never write the depfile if an error was encountered.
169
170         // 0
171         // first check if the file dependencies exist:
172         //     ->If it does exist
173         //             check if any of the files mentioned in it have
174         //             changed (done using a checksum).
175         //                 -> if changed:
176         //                        run latex once and
177         //                        remake the dependency file
178         //                 -> if not changed:
179         //                        just return there is nothing to do for us.
180         //     ->if it doesn't exist
181         //             make it and
182         //             run latex once (we need to run latex once anyway) and
183         //             remake the dependency file.
184         //
185
186         FileInfo fi(depfile);
187         bool had_depfile = fi.exist();
188         bool run_bibtex = false;
189         string aux_file = OnlyFilename(ChangeExtension(file, "aux"));
190
191         if (had_depfile) {
192                 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
193                 // Read the dep file:
194                 had_depfile = head.read(depfile);
195         }
196
197         if (had_depfile) {
198                 // Update the checksums
199                 head.update();
200                 // Can't just check if anything has changed because it might have aborted
201                 // on error last time... in which cas we need to re-run latex
202                 // and collect the error messages (even if they are the same).
203                 if (!FileInfo(output_file).exist()) {
204                         lyxerr[Debug::DEPEND]
205                                 << "re-running LaTeX because output file doesn't exist." << endl;
206                 } else if (!head.sumchange()) {
207                         lyxerr[Debug::DEPEND] << "return no_change" << endl;
208                         return NO_CHANGE;
209                 } else {
210                         lyxerr[Debug::DEPEND]
211                                 << "Dependency file has changed" << endl;
212                 }
213
214                 if (head.extchanged(".bib") || head.extchanged(".bst"))
215                         run_bibtex = true;
216         } else
217                 lyxerr[Debug::DEPEND]
218                         << "Dependency file does not exist, or has wrong format" << endl;
219
220         /// We scan the aux file even when had_depfile = false,
221         /// because we can run pdflatex on the file after running latex on it,
222         /// in which case we will not need to run bibtex again.
223         vector<Aux_Info> bibtex_info_old;
224         if (!run_bibtex)
225                 bibtex_info_old = scanAuxFiles(aux_file);
226
227         ++count;
228         lyxerr[Debug::LATEX] << "Run #" << count << endl;
229         if (lfun) {
230                 showRunMessage(lfun, count);
231         }
232
233         startscript();
234         scanres = scanLogFile(terr);
235         if (scanres & ERROR_RERUN) {
236                 lyxerr[Debug::LATEX] << "Rerunning LaTeX" << endl;
237                 startscript();
238                 scanres = scanLogFile(terr);
239         }
240
241         if (scanres & ERRORS) {
242                 deleteFilesOnError();
243                 return scanres; // return on error
244         }
245
246         vector<Aux_Info> const bibtex_info = scanAuxFiles(aux_file);
247         if (!run_bibtex && bibtex_info_old != bibtex_info)
248                 run_bibtex = true;
249
250         // update the dependencies.
251         deplog(head); // reads the latex log
252         head.update();
253
254         // 0.5
255         // At this point we must run external programs if needed.
256         // makeindex will be run if a .idx file changed or was generated.
257         // And if there were undefined citations or changes in references
258         // the .aux file is checked for signs of bibtex. Bibtex is then run
259         // if needed.
260
261         // run makeindex
262         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
263                 // no checks for now
264                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
265                 if (lfun) {
266                         lfun->dispatch(FuncRequest(LFUN_MESSAGE, _("Running MakeIndex.")));
267                 }
268
269                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
270         }
271
272         // run bibtex
273         // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
274         if (scanres & UNDEF_CIT || run_bibtex) {
275                 // Here we must scan the .aux file and look for
276                 // "\bibdata" and/or "\bibstyle". If one of those
277                 // tags is found -> run bibtex and set rerun = true;
278                 // no checks for now
279                 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
280                 if (lfun) {
281                         lfun->dispatch(FuncRequest(LFUN_MESSAGE, _("Running BibTeX.")));
282                 }
283
284                 updateBibtexDependencies(head, bibtex_info);
285                 rerun |= runBibTeX(bibtex_info);
286         } else if (!had_depfile) {
287                 /// If we run pdflatex on the file after running latex on it,
288                 /// then we do not need to run bibtex, but we do need to
289                 /// insert the .bib and .bst files into the .dep-pdf file.
290                 updateBibtexDependencies(head, bibtex_info);
291         }
292
293         // 1
294         // we know on this point that latex has been run once (or we just
295         // returned) and the question now is to decide if we need to run
296         // it any more. This is done by asking if any of the files in the
297         // dependency file has changed. (remember that the checksum for
298         // a given file is reported to have changed if it just was created)
299         //     -> if changed or rerun == true:
300         //             run latex once more and
301         //             update the dependency structure
302         //     -> if not changed:
303         //             we does nothing at this point
304         //
305         if (rerun || head.sumchange()) {
306                 rerun = false;
307                 ++count;
308                 lyxerr[Debug::DEPEND]
309                         << "Dep. file has changed or rerun requested" << endl;
310                 lyxerr[Debug::LATEX]
311                         << "Run #" << count << endl;
312                 if (lfun) {
313                         showRunMessage(lfun, count);
314                 }
315
316                 startscript();
317                 scanres = scanLogFile(terr);
318                 if (scanres & ERRORS) {
319                         deleteFilesOnError();
320                         return scanres; // return on error
321                 }
322
323                 // update the depedencies
324                 deplog(head); // reads the latex log
325                 head.update();
326         } else {
327                 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
328         }
329
330         // 1.5
331         // The inclusion of files generated by external programs like
332         // makeindex or bibtex might have done changes to pagenumbereing,
333         // etc. And because of this we must run the external programs
334         // again to make sure everything is redone correctly.
335         // Also there should be no need to run the external programs any
336         // more after this.
337
338         // run makeindex if the <file>.idx has changed or was generated.
339         if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
340                 // no checks for now
341                 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
342                 if (lfun) {
343                         lfun->dispatch(FuncRequest(LFUN_MESSAGE, _("Running MakeIndex.")));
344                 }
345
346                 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
347         }
348
349         // 2
350         // we will only run latex more if the log file asks for it.
351         // or if the sumchange() is true.
352         //     -> rerun asked for:
353         //             run latex and
354         //             remake the dependency file
355         //             goto 2 or return if max runs are reached.
356         //     -> rerun not asked for:
357         //             just return (fall out of bottom of func)
358         //
359         while ((head.sumchange() || rerun || (scanres & RERUN))
360                && count < MAX_RUN) {
361                 // Yes rerun until message goes away, or until
362                 // MAX_RUNS are reached.
363                 rerun = false;
364                 ++count;
365                 lyxerr[Debug::LATEX] << "Run #" << count << endl;
366                 if (lfun) {
367                         showRunMessage(lfun, count);
368                 }
369
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 }