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