1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich
6 * Copyright 1995-2000 The LyX Team.
8 * This file is Copyright 1996-2000
11 * ======================================================
17 #pragma implementation
21 #include "support/filetools.h"
23 #include "support/FileInfo.h"
25 #include "support/lyxlib.h"
26 #include "support/syscall.h"
27 #include "support/syscontr.h"
28 #include "support/path.h"
29 #include "support/LRegex.h"
30 #include "support/LSubstring.h"
31 #include "bufferlist.h"
32 #include "minibuffer.h"
34 #include "lyx_gui_misc.h"
40 // TODO: in no particular order
41 // - get rid of the extern BufferList and the call to
42 // BufferList::updateIncludedTeXfiles, this should either
43 // be done before calling LaTeX::funcs or in a completely
45 // - the bibtex command options should be supported.
46 // - the makeindex style files should be taken care of with
47 // the dependency mechanism.
48 // - makeindex commandline options should be supported
49 // - somewhere support viewing of bibtex and makeindex log files.
50 // - we should perhaps also scan the bibtex log file
51 // - we should perhaps also scan the bibtex log file
53 extern BufferList bufferlist;
59 void TeXErrors::insertError(int line, string const & error_desc,
60 string const & error_text)
62 Error newerr(line, error_desc, error_text);
63 errors.push_back(newerr);
70 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
71 : cmd(latex), file(f), path(p)
74 depfile = file + ".dep";
75 if (prefixIs(cmd, "pdf")) // Do we use pdflatex ?
80 void LaTeX::deleteFilesOnError() const
82 // currently just a dummy function.
84 // What files do we have to delete?
86 // This will at least make latex do all the runs
89 // but the reason for the error might be in a generated file...
91 string ofname = OnlyFilename(file);
94 string bbl = ChangeExtension(ofname, ".bbl");
98 string ind = ChangeExtension(ofname, ".ind");
101 // Also remove the aux file
102 string aux = ChangeExtension(ofname, ".aux");
107 int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
108 // We know that this function will only be run if the lyx buffer
109 // has been changed. We also know that a newly written .tex file
110 // is always different from the previous one because of the date
111 // in it. However it seems safe to run latex (at least) on time each
112 // time the .tex file changes.
114 int scanres = LaTeX::NO_ERRORS;
115 unsigned int count = 0; // number of times run
116 num_errors = 0; // just to make sure.
117 const unsigned int MAX_RUN = 6;
118 DepTable head; // empty head
119 bool rerun = false; // rerun requested
121 // The class LaTeX does not know the temp path.
122 bufferlist.updateIncludedTeXfiles(lyx::getcwd()); //GetCWD());
124 // Never write the depfile if an error was encountered.
127 // first check if the file dependencies exist:
128 // ->If it does exist
129 // check if any of the files mentioned in it have
130 // changed (done using a checksum).
132 // run latex once and
133 // remake the dependency file
134 // -> if not changed:
135 // just return there is nothing to do for us.
136 // ->if it doesn't exist
138 // run latex once (we need to run latex once anyway) and
139 // remake the dependency file.
141 FileInfo fi(depfile);
142 bool run_bibtex = false;
144 // Read the dep file:
146 // Update the checksums
149 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
150 if (head.sumchange()) {
152 lyxerr[Debug::DEPEND]
153 << "Dependency file has changed" << endl;
155 << "Run #" << count << endl;
157 string(_("LaTeX run number ")) + tostr(count));
159 scanres = scanLogFile(terr);
160 if (scanres & LaTeX::ERRORS) {
161 deleteFilesOnError();
162 return scanres; // return on error
165 run_bibtex = scanAux(head);
167 lyxerr[Debug::DEPEND]
168 << "Bibtex demands rerun" << endl;
170 lyxerr[Debug::DEPEND] << "return no_change" << endl;
171 return LaTeX::NO_CHANGE;
175 lyxerr[Debug::DEPEND]
176 << "Dependency file does not exist" << endl;
179 << "Run #" << count << endl;
180 head.insert(file, true);
182 string(_("LaTeX run number ")) + tostr(count));
184 scanres = scanLogFile(terr);
185 if (scanres & LaTeX::ERRORS) {
186 deleteFilesOnError();
187 return scanres; // return on error
192 // update the dependencies.
193 deplog(head); // reads the latex log
197 // At this point we must run external programs if needed.
198 // makeindex will be run if a .idx file changed or was generated.
199 // And if there were undefined citations or changes in references
200 // the .aux file is checked for signs of bibtex. Bibtex is then run
204 if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
206 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
207 WriteStatus(minib, _("Running MakeIndex."));
208 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
212 if (scanres & LaTeX::UNDEF_CIT
213 || scanres & LaTeX::RERUN
215 // Here we must scan the .aux file and look for
216 // "\bibdata" and/or "\bibstyle". If one of those
217 // tags is found -> run bibtex and set rerun = true;
219 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
220 WriteStatus(minib, _("Running BibTeX."));
221 rerun = runBibTeX(OnlyFilename(ChangeExtension(file, ".aux")),
226 // we know on this point that latex has been run once (or we just
227 // returned) and the question now is to decide if we need to run
228 // it any more. This is done by asking if any of the files in the
229 // dependency file has changed. (remember that the checksum for
230 // a given file is reported to have changed if it just was created)
231 // -> if changed or rerun == true:
232 // run latex once more and
233 // update the dependency structure
234 // -> if not changed:
235 // we does nothing at this point
237 if (rerun || head.sumchange()) {
240 lyxerr[Debug::DEPEND]
241 << "Dep. file has changed or rerun requested" << endl;
243 << "Run #" << count << endl;
245 string(_("LaTeX run number ")) + tostr(count));
247 scanres = scanLogFile(terr);
248 if (scanres & LaTeX::ERRORS) {
249 deleteFilesOnError();
250 return scanres; // return on error
253 // update the depedencies
254 deplog(head); // reads the latex log
257 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
261 // The inclusion of files generated by external programs like
262 // makeindex or bibtex might have done changes to pagenumbereing,
263 // etc. And because of this we must run the external programs
264 // again to make sure everything is redone correctly.
265 // Also there should be no need to run the external programs any
268 // run makeindex if the <file>.idx has changed or was generated.
269 if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
271 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
272 WriteStatus(minib, _("Running MakeIndex."));
273 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
277 // we will only run latex more if the log file asks for it.
278 // or if the sumchange() is true.
279 // -> rerun asked for:
281 // remake the dependency file
282 // goto 2 or return if max runs are reached.
283 // -> rerun not asked for:
284 // just return (fall out of bottom of func)
286 while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN))
287 && count < MAX_RUN) {
288 // Yes rerun until message goes away, or until
289 // MAX_RUNS are reached.
292 lyxerr[Debug::LATEX] << "Run #" << count << endl;
293 WriteStatus(minib, string(_("LaTeX run number ")) + tostr(count));
295 scanres = scanLogFile(terr);
296 if (scanres & LaTeX::ERRORS) {
297 deleteFilesOnError();
298 return scanres; // return on error
305 // Write the dependencies to file.
307 lyxerr[Debug::LATEX] << "Done." << endl;
312 int LaTeX::operator()()
315 string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
316 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
317 string tmp = cmd + ' ' + file + " > nul";
320 return one.startscript(Systemcalls::System, tmp);
324 bool LaTeX::runMakeIndex(string const & f)
326 lyxerr[Debug::LATEX] << "idx file has been made,"
327 " running makeindex on file "
330 // It should be possible to set the switches for makeindex
331 // sorting style and such. It would also be very convenient
332 // to be able to make style files from within LyX. This has
333 // to come for a later time. (0.13 perhaps?)
334 string tmp = "makeindex -c -q ";
337 one.startscript(Systemcalls::System, tmp);
342 bool LaTeX::scanAux(DepTable & dep)
344 // if any of the bib file has changed we don't have to
345 // check the .aux file.
346 if (dep.extchanged(".bib")
347 || dep.extchanged(".bst")) return true;
349 string aux = OnlyFilename(ChangeExtension(file, ".aux"));
350 ifstream ifs(aux.c_str());
352 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
353 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
354 while (getline(ifs, token)) {
355 if (reg1.exact_match(token)) {
356 LRegex::SubMatches sub = reg1.exec(token);
357 string data = LSubstring(token, sub[1].first,
361 b = data.find_first_of(',', 0);
363 if (b == string::npos)
366 l = data.substr( 0, b - 0);
367 data.erase(0, b + 1);
371 ChangeExtension(l, "bib"), "bib");
372 if (!full_l.empty()) {
373 if (!dep.exist(full_l))
376 } while (b != string::npos);
377 } else if (reg2.exact_match(token)) {
378 LRegex::SubMatches sub = reg2.exec(token);
379 string style = LSubstring(token, sub[1].first,
381 // token is now the style file
382 // pass it to the helper
385 ChangeExtension(style, "bst"),
387 if (!full_l.empty()) {
388 if (!dep.exist(full_l))
397 bool LaTeX::runBibTeX(string const & f, DepTable & dep)
399 // Since a run of Bibtex mandates more latex runs it is ok to
400 // remove all ".bib" and ".bst" files, it is also required to
401 // discover style and database changes.
402 dep.remove_files_with_extension(".bib");
403 dep.remove_files_with_extension(".bst");
404 ifstream ifs(f.c_str());
406 bool using_bibtex = false;
407 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
408 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
409 while (getline(ifs, token)) {
410 if (reg1.exact_match(token)) {
412 LRegex::SubMatches const & sub = reg1.exec(token);
413 string data = LSubstring(token, sub[1].first,
415 // data is now all the bib files separated by ','
416 // get them one by one and pass them to the helper
419 b = data.find_first_of(',', 0);
421 if (b == string::npos)
424 l = data.substr(0, b - 0);
425 data.erase(0, b + 1);
429 ChangeExtension(l, "bib"),
431 lyxerr[Debug::LATEX] << "Bibtex database: `"
432 << full_l << "'" << endl;
433 if (!full_l.empty()) {
434 // add full_l to the dep file.
435 dep.insert(full_l, true);
437 } while (b != string::npos);
438 } else if (reg2.exact_match(token)) {
440 LRegex::SubMatches const & sub = reg2.exec(token);
441 string style = LSubstring(token, sub[1].first,
443 // token is now the style file
444 // pass it to the helper
447 ChangeExtension(style, "bst"),
449 lyxerr[Debug::LATEX] << "Bibtex style: `"
450 << full_l << "'" << endl;
451 if (!full_l.empty()) {
452 // add full_l to the dep file.
453 dep.insert(full_l, true);
459 string tmp = "bibtex ";
460 tmp += OnlyFilename(ChangeExtension(file, string()));
462 one.startscript(Systemcalls::System, tmp);
465 // bibtex was not run.
470 int LaTeX::scanLogFile(TeXErrors & terr)
474 int retval = NO_ERRORS;
475 string tmp = OnlyFilename(ChangeExtension(file, ".log"));
476 lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
477 ifstream ifs(tmp.c_str());
480 while (getline(ifs, token)) {
481 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
486 if (prefixIs(token, "LaTeX Warning:")) {
487 // Here shall we handle different
489 retval |= LATEX_WARNING;
490 lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
491 if (contains(token, "Rerun to get cross-references")) {
494 << "We should rerun." << endl;
495 } else if (contains(token, "Citation")
496 && contains(token, "on page")
497 && contains(token, "undefined")) {
500 } else if (prefixIs(token, "Package")) {
502 retval |= PACKAGE_WARNING;
503 if (contains(token, "natbib Warning:")) {
505 if (contains(token, "Citation")
506 && contains(token, "on page")
507 && contains(token, "undefined")) {
510 } else if (contains(token, "Rerun LaTeX.")) {
511 // at least longtable.sty might use this.
514 } else if (prefixIs(token, "! ")) {
515 // Ok, we have something that looks like a TeX Error
516 // but what do we really have.
518 // Just get the error description:
519 string desc(token, 2);
520 if (contains(token, "LaTeX Error:"))
521 retval |= LATEX_ERROR;
526 if (!getline(ifs, tmp))
530 } while (!prefixIs(tmp, "l."));
531 if (prefixIs(tmp, "l.")) {
532 // we have a latex error
534 // get the line number:
536 sscanf(tmp.c_str(), "l.%d", &line);
537 // get the rest of the message:
538 string errstr(tmp, tmp.find(' '));
541 while (!contains(errstr, "l.")
543 && !prefixIs(tmp, "! ")
544 && !contains(tmp, "(job aborted")) {
550 << "line: " << line << '\n'
551 << "Desc: " << desc << '\n'
552 << "Text: " << errstr << endl;
553 if (line == last_line)
559 if (line_count <= 5) {
560 terr.insertError(line, desc, errstr);
565 // information messages, TeX warnings and other
566 // warnings we have not caught earlier.
567 if (prefixIs(token, "Overfull ")) {
568 retval |= TEX_WARNING;
569 } else if (prefixIs(token, "Underfull ")) {
570 retval |= TEX_WARNING;
571 } else if (contains(token, "Rerun to get citations")) {
572 // Natbib seems to use this.
574 } else if (contains(token, "No pages of output")) {
575 // A dvi file was not created
577 } else if (contains(token, "That makes 100 errors")) {
578 // More than 100 errors were reprted
579 retval |= TOO_MANY_ERRORS;
583 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
588 void LaTeX::deplog(DepTable & head)
590 // This function reads the LaTeX log file end extracts all the external
591 // files used by the LaTeX run. The files are then entered into the
594 string logfile = OnlyFilename(ChangeExtension(file, ".log"));
596 LRegex reg1(")* *\\(([^ \\)]+).*");
597 LRegex reg2("File: ([^ ]+).*");
598 LRegex reg3("No file ([^ ]+)\\..*");
599 LRegex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
600 LRegex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
602 ifstream ifs(logfile.c_str());
604 // Ok, the scanning of files here is not sufficient.
605 // Sometimes files are named by "File: xxx" only
606 // So I think we should use some regexps to find files instead.
607 // "(\([^ ]+\)" should match the "(file " variant
608 // "File: \([^ ]+\)" should match the "File: file" variant
612 if (token.empty()) continue;
614 if (reg1.exact_match(token)) {
615 LRegex::SubMatches const & sub = reg1.exec(token);
616 foundfile = LSubstring(token, sub[1].first,
618 } else if (reg2.exact_match(token)) {
619 LRegex::SubMatches const & sub = reg2.exec(token);
620 foundfile = LSubstring(token, sub[1].first,
622 } else if (reg3.exact_match(token)) {
623 LRegex::SubMatches const & sub = reg3.exec(token);
624 foundfile = LSubstring(token, sub[1].first,
626 } else if (reg4.exact_match(token)) {
627 LRegex::SubMatches const & sub = reg4.exec(token);
628 foundfile = LSubstring(token, sub[1].first,
634 lyxerr[Debug::DEPEND] << "Found file: "
635 << foundfile << endl;
637 // Ok now we found a file.
638 // Now we should make sure that this is a file that we can
639 // access through the normal paths.
640 // We will not try any fancy search methods to
643 // (1) foundfile is an
644 // absolute path and should
646 if (AbsolutePath(foundfile)) {
647 lyxerr[Debug::DEPEND] << "AbsolutePath file: "
648 << foundfile << endl;
649 // On initial insert we want to do the update at once
650 // since this file can not be a file generated by
652 head.insert(foundfile, true);
656 // (2) foundfile is in the tmpdir
657 // insert it into head
658 if (FileInfo(OnlyFilename(foundfile)).exist()) {
659 if (unwanted.exact_match(foundfile)) {
660 lyxerr[Debug::DEPEND]
662 << OnlyFilename(foundfile)
663 << " in the dep file"
665 } else if (suffixIs(foundfile, ".tex")) {
666 // This is a tex file generated by LyX
667 // and latex is not likely to change this
669 lyxerr[Debug::DEPEND]
670 << "Tmpdir TeX file: "
671 << OnlyFilename(foundfile)
673 head.insert(foundfile, true);
675 lyxerr[Debug::DEPEND]
677 << OnlyFilename(foundfile)
679 head.insert(OnlyFilename(foundfile));
683 lyxerr[Debug::DEPEND]
684 << "Not a file or we are unable to find it."