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