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"
39 // TODO: in no particular order
40 // - get rid of the extern BufferList and the call to
41 // BufferList::updateIncludedTeXfiles, this should either
42 // be done before calling LaTeX::funcs or in a completely
44 // - the bibtex command options should be supported.
45 // - the makeindex style files should be taken care of with
46 // the dependency mechanism.
47 // - makeindex commandline options should be supported
48 // - somewhere support viewing of bibtex and makeindex log files.
49 // - we should perhaps also scan the bibtex log file
50 // - we should perhaps also scan the bibtex log file
52 extern BufferList bufferlist;
58 void TeXErrors::insertError(int line, string const & error_desc,
59 string const & error_text)
61 Error newerr(line, error_desc, error_text);
62 errors.push_back(newerr);
69 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
70 : cmd(latex), file(f), path(p)
73 depfile = file + ".dep";
74 if (prefixIs(cmd, "pdf")) // Do we use pdflatex ?
79 void LaTeX::deleteFilesOnError() const
81 // currently just a dummy function.
83 // What files do we have to delete?
85 // This will at least make latex do all the runs
88 // but the reason for the error might be in a generated file...
90 string ofname = OnlyFilename(file);
93 string bbl = ChangeExtension(ofname, ".bbl");
97 string ind = ChangeExtension(ofname, ".ind");
100 // Also remove the aux file
101 string aux = ChangeExtension(ofname, ".aux");
106 int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
107 // We know that this function will only be run if the lyx buffer
108 // has been changed. We also know that a newly written .tex file
109 // is always different from the previous one because of the date
110 // in it. However it seems safe to run latex (at least) on time each
111 // time the .tex file changes.
113 int scanres = LaTeX::NO_ERRORS;
114 unsigned int count = 0; // number of times run
115 num_errors = 0; // just to make sure.
116 const unsigned int MAX_RUN = 6;
117 DepTable head; // empty head
118 bool rerun = false; // rerun requested
120 // The class LaTeX does not know the temp path.
121 bufferlist.updateIncludedTeXfiles(GetCWD());
123 // Never write the depfile if an error was encountered.
126 // first check if the file dependencies exist:
127 // ->If it does exist
128 // check if any of the files mentioned in it have
129 // changed (done using a checksum).
131 // run latex once and
132 // remake the dependency file
133 // -> if not changed:
134 // just return there is nothing to do for us.
135 // ->if it doesn't exist
137 // run latex once (we need to run latex once anyway) and
138 // remake the dependency file.
140 FileInfo fi(depfile);
141 bool run_bibtex = false;
143 // Read the dep file:
145 // Update the checksums
148 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
149 if (head.sumchange()) {
151 lyxerr[Debug::DEPEND]
152 << "Dependency file has changed" << endl;
154 << "Run #" << count << endl;
155 minib->Set(string(_("LaTeX run number ")) + tostr(count));
158 scanres = scanLogFile(terr);
159 if (scanres & LaTeX::ERRORS) {
160 deleteFilesOnError();
161 return scanres; // return on error
164 run_bibtex = scanAux(head);
166 lyxerr[Debug::DEPEND]
167 << "Bibtex demands rerun" << endl;
169 lyxerr[Debug::DEPEND] << "return no_change" << endl;
170 return LaTeX::NO_CHANGE;
174 lyxerr[Debug::DEPEND]
175 << "Dependency file does not exist" << endl;
178 << "Run #" << count << endl;
179 head.insert(file, true);
180 minib->Set(string(_("LaTeX run number ")) + tostr(count));
183 scanres = scanLogFile(terr);
184 if (scanres & LaTeX::ERRORS) {
185 deleteFilesOnError();
186 return scanres; // return on error
191 // update the dependencies.
192 deplog(head); // reads the latex log
196 // At this point we must run external programs if needed.
197 // makeindex will be run if a .idx file changed or was generated.
198 // And if there were undefined citations or changes in references
199 // the .aux file is checked for signs of bibtex. Bibtex is then run
203 if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
205 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
206 minib->Set(_("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 minib->Set(_("Running BibTeX."));
222 rerun = runBibTeX(OnlyFilename(ChangeExtension(file, ".aux")),
227 // we know on this point that latex has been run once (or we just
228 // returned) and the question now is to decide if we need to run
229 // it any more. This is done by asking if any of the files in the
230 // dependency file has changed. (remember that the checksum for
231 // a given file is reported to have changed if it just was created)
232 // -> if changed or rerun == true:
233 // run latex once more and
234 // update the dependency structure
235 // -> if not changed:
236 // we does nothing at this point
238 if (rerun || head.sumchange()) {
241 lyxerr[Debug::DEPEND]
242 << "Dep. file has changed or rerun requested" << endl;
244 << "Run #" << count << endl;
245 minib->Set(string(_("LaTeX run number ")) + tostr(count));
248 scanres = scanLogFile(terr);
249 if (scanres & LaTeX::ERRORS) {
250 deleteFilesOnError();
251 return scanres; // return on error
254 // update the depedencies
255 deplog(head); // reads the latex log
258 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
262 // The inclusion of files generated by external programs like
263 // makeindex or bibtex might have done changes to pagenumbereing,
264 // etc. And because of this we must run the external programs
265 // again to make sure everything is redone correctly.
266 // Also there should be no need to run the external programs any
269 // run makeindex if the <file>.idx has changed or was generated.
270 if (head.haschanged(OnlyFilename(ChangeExtension(file, ".idx")))) {
272 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
273 minib->Set(_("Running MakeIndex."));
275 rerun = runMakeIndex(OnlyFilename(ChangeExtension(file, ".idx")));
279 // we will only run latex more if the log file asks for it.
280 // or if the sumchange() is true.
281 // -> rerun asked for:
283 // remake the dependency file
284 // goto 2 or return if max runs are reached.
285 // -> rerun not asked for:
286 // just return (fall out of bottom of func)
288 while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN))
289 && count < MAX_RUN) {
290 // Yes rerun until message goes away, or until
291 // MAX_RUNS are reached.
294 lyxerr[Debug::LATEX] << "Run #" << count << endl;
295 minib->Set(string(_("LaTeX run number ")) + tostr(count));
298 scanres = scanLogFile(terr);
299 if (scanres & LaTeX::ERRORS) {
300 deleteFilesOnError();
301 return scanres; // return on error
308 // Write the dependencies to file.
310 lyxerr[Debug::LATEX] << "Done." << endl;
315 int LaTeX::operator()()
318 string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
319 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
320 string tmp = cmd + ' ' + file + " > nul";
323 return one.startscript(Systemcalls::System, tmp);
327 bool LaTeX::runMakeIndex(string const & f)
329 lyxerr[Debug::LATEX] << "idx file has been made,"
330 " running makeindex on file "
333 // It should be possible to set the switches for makeindex
334 // sorting style and such. It would also be very convenient
335 // to be able to make style files from within LyX. This has
336 // to come for a later time. (0.13 perhaps?)
337 string tmp = "makeindex -c -q ";
340 one.startscript(Systemcalls::System, tmp);
345 bool LaTeX::scanAux(DepTable & dep)
347 // if any of the bib file has changed we don't have to
348 // check the .aux file.
349 if (dep.extchanged(".bib")
350 || dep.extchanged(".bst")) return true;
352 string aux = OnlyFilename(ChangeExtension(file, ".aux"));
353 ifstream ifs(aux.c_str());
355 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
356 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
357 while (getline(ifs, token)) {
358 if (reg1.exact_match(token)) {
359 LRegex::SubMatches sub = reg1.exec(token);
360 string data = LSubstring(token, sub[1].first,
364 b = data.find_first_of(',', 0);
366 if (b == string::npos)
369 l = data.substr( 0, b - 0);
370 data.erase(0, b + 1);
374 ChangeExtension(l, "bib"), "bib");
375 if (!full_l.empty()) {
376 if (!dep.exist(full_l))
379 } while (b != string::npos);
380 } else if (reg2.exact_match(token)) {
381 LRegex::SubMatches sub = reg2.exec(token);
382 string style = LSubstring(token, sub[1].first,
384 // token is now the style file
385 // pass it to the helper
388 ChangeExtension(style, "bst"),
390 if (!full_l.empty()) {
391 if (!dep.exist(full_l))
400 bool LaTeX::runBibTeX(string const & f, DepTable & dep)
402 // Since a run of Bibtex mandates more latex runs it is ok to
403 // remove all ".bib" and ".bst" files, it is also required to
404 // discover style and database changes.
405 dep.remove_files_with_extension(".bib");
406 dep.remove_files_with_extension(".bst");
407 ifstream ifs(f.c_str());
409 bool using_bibtex = false;
410 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
411 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
412 while (getline(ifs, token)) {
413 if (reg1.exact_match(token)) {
415 LRegex::SubMatches const & sub = reg1.exec(token);
416 string data = LSubstring(token, sub[1].first,
418 // data is now all the bib files separated by ','
419 // get them one by one and pass them to the helper
422 b = data.find_first_of(',', 0);
424 if (b == string::npos)
427 l = data.substr(0, b - 0);
428 data.erase(0, b + 1);
432 ChangeExtension(l, "bib"),
434 lyxerr[Debug::LATEX] << "Bibtex database: `"
435 << full_l << "'" << endl;
436 if (!full_l.empty()) {
437 // add full_l to the dep file.
438 dep.insert(full_l, true);
440 } while (b != string::npos);
441 } else if (reg2.exact_match(token)) {
443 LRegex::SubMatches const & sub = reg2.exec(token);
444 string style = LSubstring(token, sub[1].first,
446 // token is now the style file
447 // pass it to the helper
450 ChangeExtension(style, "bst"),
452 lyxerr[Debug::LATEX] << "Bibtex style: `"
453 << full_l << "'" << endl;
454 if (!full_l.empty()) {
455 // add full_l to the dep file.
456 dep.insert(full_l, true);
462 string tmp = "bibtex ";
463 tmp += OnlyFilename(ChangeExtension(file, string()));
465 one.startscript(Systemcalls::System, tmp);
468 // bibtex was not run.
473 int LaTeX::scanLogFile(TeXErrors & terr)
477 int retval = NO_ERRORS;
478 string tmp = OnlyFilename(ChangeExtension(file, ".log"));
479 lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
480 ifstream ifs(tmp.c_str());
483 while (getline(ifs, token)) {
484 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
489 if (prefixIs(token, "LaTeX Warning:")) {
490 // Here shall we handle different
492 retval |= LATEX_WARNING;
493 lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
494 if (contains(token, "Rerun to get cross-references")) {
497 << "We should rerun." << endl;
498 } else if (contains(token, "Citation")
499 && contains(token, "on page")
500 && contains(token, "undefined")) {
503 } else if (prefixIs(token, "Package")) {
505 retval |= PACKAGE_WARNING;
506 if (contains(token, "natbib Warning:")) {
508 if (contains(token, "Citation")
509 && contains(token, "on page")
510 && contains(token, "undefined")) {
513 } else if (contains(token, "Rerun LaTeX.")) {
514 // at least longtable.sty might use this.
517 } else if (prefixIs(token, "! ")) {
518 // Ok, we have something that looks like a TeX Error
519 // but what do we really have.
521 // Just get the error description:
522 string desc(token, 2);
523 if (contains(token, "LaTeX Error:"))
524 retval |= LATEX_ERROR;
529 if (!getline(ifs, tmp))
533 } while (!prefixIs(tmp, "l."));
534 if (prefixIs(tmp, "l.")) {
535 // we have a latex error
537 // get the line number:
539 sscanf(tmp.c_str(), "l.%d", &line);
540 // get the rest of the message:
541 string errstr(tmp, tmp.find(' '));
544 while (!contains(errstr, "l.")
546 && !prefixIs(tmp, "! ")
547 && !contains(tmp, "(job aborted")) {
553 << "line: " << line << '\n'
554 << "Desc: " << desc << '\n'
555 << "Text: " << errstr << endl;
556 if (line == last_line)
562 if (line_count <= 5) {
563 terr.insertError(line, desc, errstr);
568 // information messages, TeX warnings and other
569 // warnings we have not caught earlier.
570 if (prefixIs(token, "Overfull ")) {
571 retval |= TEX_WARNING;
572 } else if (prefixIs(token, "Underfull ")) {
573 retval |= TEX_WARNING;
574 } else if (contains(token, "Rerun to get citations")) {
575 // Natbib seems to use this.
577 } else if (contains(token, "No pages of output")) {
578 // A dvi file was not created
580 } else if (contains(token, "That makes 100 errors")) {
581 // More than 100 errors were reprted
582 retval |= TOO_MANY_ERRORS;
586 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
591 void LaTeX::deplog(DepTable & head)
593 // This function reads the LaTeX log file end extracts all the external
594 // files used by the LaTeX run. The files are then entered into the
597 string logfile = OnlyFilename(ChangeExtension(file, ".log"));
599 LRegex reg1(")* *\\(([^ ]+).*");
600 LRegex reg2("File: ([^ ]+).*");
601 LRegex reg3("No file ([^ ]+)\\..*");
602 LRegex reg4("\\\\openout[0-9]+.*=.*`([^ ]+)'\\..*");
603 LRegex unwanted("^.*\\.(aux|log|dvi|bbl|ind|glo)$");
605 ifstream ifs(logfile.c_str());
607 // Ok, the scanning of files here is not sufficient.
608 // Sometimes files are named by "File: xxx" only
609 // So I think we should use some regexps to find files instead.
610 // "(\([^ ]+\)" should match the "(file " variant
611 // "File: \([^ ]+\)" should match the "File: file" variant
615 if (token.empty()) continue;
617 if (reg1.exact_match(token)) {
618 LRegex::SubMatches const & sub = reg1.exec(token);
619 foundfile = LSubstring(token, sub[1].first,
621 } else if (reg2.exact_match(token)) {
622 LRegex::SubMatches const & sub = reg2.exec(token);
623 foundfile = LSubstring(token, sub[1].first,
625 } else if (reg3.exact_match(token)) {
626 LRegex::SubMatches const & sub = reg3.exec(token);
627 foundfile = LSubstring(token, sub[1].first,
629 } else if (reg4.exact_match(token)) {
630 LRegex::SubMatches const & sub = reg4.exec(token);
631 foundfile = LSubstring(token, sub[1].first,
637 lyxerr[Debug::DEPEND] << "Found file: "
638 << foundfile << endl;
640 // Ok now we found a file.
641 // Now we should make sure that this is a file that we can
642 // access through the normal paths.
643 // We will not try any fance search methods to
646 // (1) foundfile is an
647 // absolute path and should
649 if (AbsolutePath(foundfile)) {
650 lyxerr[Debug::DEPEND] << "AbsolutePath file: "
651 << foundfile << endl;
652 // On inital insert we want to do the update at once
653 // since this file can not be a file generated by
655 head.insert(foundfile, true);
659 // (2) foundfile is in the tmpdir
660 // insert it into head
661 if (FileInfo(OnlyFilename(foundfile)).exist()) {
662 if (unwanted.exact_match(foundfile)) {
663 lyxerr[Debug::DEPEND]
665 << OnlyFilename(foundfile)
666 << " in the dep file"
668 } else if (suffixIs(foundfile, ".tex")) {
669 // This is a tex file generated by LyX
670 // and latex is not likely to change this
672 lyxerr[Debug::DEPEND]
673 << "Tmpdir TeX file: "
674 << OnlyFilename(foundfile)
676 head.insert(foundfile, true);
678 lyxerr[Debug::DEPEND]
680 << OnlyFilename(foundfile)
682 head.insert(OnlyFilename(foundfile));
686 lyxerr[Debug::DEPEND]
687 << "Not a file or we are unable to find it."