1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich
6 * Copyright 1995-1999 The LyX Team.
8 * This file is Copyright 1996-1999
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"
37 // TODO: in no particular order
38 // - get rid of the extern BufferList and the call to
39 // BufferList::updateIncludedTeXfiles, this should either
40 // be done before calling LaTeX::funcs or in a completely
42 // - the bibtex command options should be supported.
43 // - the makeindex style files should be taken care of with
44 // the dependency mechanism.
45 // - makeindex commandline options should be supported
46 // - somewhere support viewing of bibtex and makeindex log files.
47 // - we should perhaps also scan the bibtex log file
48 // - we should perhaps also scan the bibtex log file
50 extern BufferList bufferlist;
52 struct texfile_struct {
53 LaTeX::TEX_FILES file;
54 char const * extension;
58 const texfile_struct all_files[] = {
59 { LaTeX::AUX, ".aux"},
60 { LaTeX::BBL, ".bbl"},
61 { LaTeX::DVI, ".dvi"},
62 { LaTeX::GLO, ".glo"},
63 { LaTeX::IDX, ".idx"},
64 { LaTeX::IND, ".ind"},
65 { LaTeX::LOF, ".lof"},
66 { LaTeX::LOA, ".loa"},
67 { LaTeX::LOG, ".log"},
68 { LaTeX::LOT, ".lot"},
69 { LaTeX::TOC, ".toc"},
70 { LaTeX::LTX, ".ltx"},
79 void TeXErrors::insertError(int line, string const & error_desc,
80 string const & error_text)
82 Error newerr(line, error_desc, error_text);
83 errors.push_back(newerr);
90 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
91 : cmd(latex), file(f), path(p)
94 file_count = sizeof(all_files) / sizeof(texfile_struct);
96 depfile = file + ".dep";
100 int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
101 // We know that this function will only be run if the lyx buffer
102 // has been changed. We also know that a newly written .tex file
103 // is always different from the previous one because of the date
104 // in it. However it seems safe to run latex (at least) on time each
105 // time the .tex file changes.
107 int scanres = LaTeX::NO_ERRORS;
108 unsigned int count = 0; // number of times run
109 num_errors = 0; // just to make sure.
110 const unsigned int MAX_RUN = 6;
111 DepTable head; // empty head
112 bool rerun = false; // rerun requested
114 // The class LaTeX does not know the temp path.
115 bufferlist.updateIncludedTeXfiles(GetCWD());
117 // Never write the depfile if an error was encountered.
120 // first check if the file dependencies exist:
121 // ->If it does exist
122 // check if any of the files mentioned in it have
123 // changed (done using a checksum).
125 // run latex once and
126 // remake the dependency file
127 // -> if not changed:
128 // just return there is nothing to do for us.
129 // ->if it doesn't exist
131 // run latex once (we need to run latex once anyway) and
132 // remake the dependency file.
134 FileInfo fi(depfile);
135 bool run_bibtex = false;
137 // Read the dep file:
139 // Update the checksums
142 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
143 if (head.sumchange()) {
145 lyxerr[Debug::DEPEND]
146 << "Dependency file has changed" << endl;
148 << "Run #" << count << endl;
149 minib->Set(string(_("LaTeX run number ")) + tostr(count));
152 scanres = scanLogFile(terr);
153 if (scanres & LaTeX::ERRORS) return scanres; // return on error
154 run_bibtex = scanAux(head);
156 lyxerr[Debug::DEPEND]
157 << "Bibtex demands rerun" << endl;
159 lyxerr[Debug::DEPEND] << "return no_change" << endl;
160 return LaTeX::NO_CHANGE;
164 lyxerr[Debug::DEPEND]
165 << "Dependency file does not exist" << endl;
167 << "Run #" << count << endl;
168 head.insert(file, true);
169 minib->Set(string(_("LaTeX run number ")) + tostr(count));
172 scanres = scanLogFile(terr);
173 if (scanres & LaTeX::ERRORS) return scanres; // return on error
176 // update the dependencies.
177 deplog(head); // reads the latex log
178 deptex(head); // checks for latex files
182 // At this point we must run external programs if needed.
183 // makeindex will be run if a .idx file changed or was generated.
184 // And if there were undefined citations or changes in references
185 // the .aux file is checked for signs of bibtex. Bibtex is then run
189 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
191 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
192 minib->Set(_("Running MakeIndex."));
194 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
198 if (scanres & LaTeX::UNDEF_CIT
199 || scanres & LaTeX::RERUN
201 // Here we must scan the .aux file and look for
202 // "\bibdata" and/or "\bibstyle". If one of those
203 // tags is found -> run bibtex and set rerun = true;
205 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
206 minib->Set(_("Running BibTeX."));
208 rerun = runBibTeX(ChangeExtension(file, ".aux", true), head);
212 // we know on this point that latex has been run once (or we just
213 // returned) and the question now is to decide if we need to run
214 // it any more. This is done by asking if any of the files in the
215 // dependency file has changed. (remember that the checksum for
216 // a given file is reported to have changed if it just was created)
217 // -> if changed or rerun == true:
218 // run latex once more and
219 // update the dependency structure
220 // -> if not changed:
221 // we does nothing at this point
223 if (rerun || head.sumchange()) {
226 lyxerr[Debug::DEPEND]
227 << "Dep. file has changed or rerun requested" << endl;
229 << "Run #" << count << endl;
230 minib->Set(string(_("LaTeX run number ")) + tostr(count));
233 scanres = scanLogFile(terr);
234 if (scanres & LaTeX::ERRORS) return scanres; // return on error
235 // update the depedencies
236 deplog(head); // reads the latex log
239 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
243 // The inclusion of files generated by external programs like
244 // makeindex or bibtex might have done changes to pagenumbereing,
245 // etc. And because of this we must run the external programs
246 // again to make sure everything is redone correctly.
247 // Also there should be no need to run the external programs any
250 // run makeindex if the <file>.idx has changed or was generated.
251 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
253 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
254 minib->Set(_("Running MakeIndex."));
256 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
260 // we will only run latex more if the log file asks for it.
261 // or if the sumchange() is true.
262 // -> rerun asked for:
264 // remake the dependency file
265 // goto 2 or return if max runs are reached.
266 // -> rerun not asked for:
267 // just return (fall out of bottom of func)
269 while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN))
270 && count < MAX_RUN) {
271 // Yes rerun until message goes away, or until
272 // MAX_RUNS are reached.
275 lyxerr[Debug::LATEX] << "Run #" << count << endl;
276 minib->Set(string(_("LaTeX run number ")) + tostr(count));
279 scanres = scanLogFile(terr);
280 if (scanres & LaTeX::ERRORS) return scanres; // return on error
285 // Write the dependencies to file.
287 lyxerr[Debug::LATEX] << "Done." << endl;
292 int LaTeX::operator()()
295 string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
296 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
297 string tmp = cmd + ' ' + file + " > nul";
300 return one.startscript(Systemcalls::System, tmp);
304 bool LaTeX::runMakeIndex(string const & f)
306 lyxerr[Debug::LATEX] << "idx file has been made,"
307 " running makeindex on file "
310 // It should be possible to set the switches for makeindex
311 // sorting style and such. It would also be very convenient
312 // to be able to make style files from within LyX. This has
313 // to come for a later time. (0.13 perhaps?)
314 string tmp = "makeindex -c -q ";
317 one.startscript(Systemcalls::System, tmp);
322 bool LaTeX::scanAux(DepTable & dep)
324 // if any of the bib file has changed we don't have to
325 // check the .aux file.
326 if (dep.extchanged(".bib")
327 || dep.extchanged(".bst")) return true;
329 string aux = ChangeExtension(file, ".aux", true);
330 ifstream ifs(aux.c_str());
332 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
333 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
334 while (getline(ifs, token)) {
335 if (reg1.exact_match(token)) {
336 LRegex::SubMatches sub = reg1.exec(token);
337 string data = LSubstring(token, sub[1].first,
341 b = data.find_first_of(',', 0);
343 if (b == string::npos)
346 l = data.substr( 0, b - 0);
347 data.erase(0, b + 1);
351 ChangeExtension(l, "bib", false), "bib");
352 if (!full_l.empty()) {
353 if (!dep.exist(full_l))
356 } while (b != string::npos);
357 } else if (reg2.exact_match(token)) {
358 LRegex::SubMatches sub = reg2.exec(token);
359 string style = LSubstring(token, sub[1].first,
361 // token is now the style file
362 // pass it to the helper
365 ChangeExtension(style, "bst", false),
367 if (!full_l.empty()) {
368 if (!dep.exist(full_l))
377 bool LaTeX::runBibTeX(string const & f, DepTable & dep)
379 // Since a run of Bibtex mandates more latex runs it is ok to
380 // remove all ".bib" and ".bst" files, it is also required to
381 // discover style and database changes.
382 dep.remove_files_with_extension(".bib");
383 dep.remove_files_with_extension(".bst");
384 ifstream ifs(f.c_str());
386 bool using_bibtex = false;
387 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
388 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
389 while (getline(ifs, token)) {
390 if (reg1.exact_match(token)) {
392 LRegex::SubMatches const & sub = reg1.exec(token);
393 string data = LSubstring(token, sub[1].first,
395 // data is now all the bib files separated by ','
396 // get them one by one and pass them to the helper
399 b = data.find_first_of(',', 0);
401 if (b == string::npos)
404 l = data.substr(0, b - 0);
405 data.erase(0, b + 1);
409 ChangeExtension(l, "bib", false),
411 lyxerr[Debug::LATEX] << "Bibtex database: `"
412 << full_l << "'" << endl;
413 if (!full_l.empty()) {
414 // add full_l to the dep file.
415 dep.insert(full_l, true);
417 } while (b != string::npos);
418 } else if (reg2.exact_match(token)) {
420 LRegex::SubMatches const & sub = reg2.exec(token);
421 string style = LSubstring(token, sub[1].first,
423 // token is now the style file
424 // pass it to the helper
427 ChangeExtension(style, "bst", false),
429 lyxerr[Debug::LATEX] << "Bibtex style: `"
430 << full_l << "'" << endl;
431 if (!full_l.empty()) {
432 // add full_l to the dep file.
433 dep.insert(full_l, true);
439 string tmp = "bibtex ";
440 tmp += ChangeExtension(file, string(), true);
442 one.startscript(Systemcalls::System, tmp);
445 // bibtex was not run.
450 int LaTeX::scanLogFile(TeXErrors & terr)
454 int retval = NO_ERRORS;
455 string tmp = ChangeExtension(file, ".log", true);
456 lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
457 ifstream ifs(tmp.c_str());
460 while (getline(ifs, token)) {
461 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
466 if (prefixIs(token, "LaTeX Warning:")) {
467 // Here shall we handle different
469 retval |= LATEX_WARNING;
470 lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
471 if (contains(token, "Rerun to get cross-references")) {
474 << "We should rerun." << endl;
475 } else if (contains(token, "Citation")
476 && contains(token, "on page")
477 && contains(token, "undefined")) {
480 } else if (prefixIs(token, "Package")) {
482 retval |= PACKAGE_WARNING;
483 if (contains(token, "natbib Warning:")) {
485 if (contains(token, "Citation")
486 && contains(token, "on page")
487 && contains(token, "undefined")) {
490 } else if (contains(token, "Rerun LaTeX.")) {
491 // at least longtable.sty might use this.
494 } else if (prefixIs(token, "! ")) {
495 // Ok, we have something that looks like a TeX Error
496 // but what do we really have.
498 // Just get the error description:
499 string desc(token, 2);
500 if (contains(token, "LaTeX Error:"))
501 retval |= LATEX_ERROR;
506 if (!getline(ifs, tmp))
510 } while (!prefixIs(tmp, "l."));
511 if (prefixIs(tmp, "l.")) {
512 // we have a latex error
514 // get the line number:
516 sscanf(tmp.c_str(), "l.%d", &line);
517 // get the rest of the message:
518 string errstr(tmp, tmp.find(' '));
521 while (!contains(errstr, "l.")
523 && !prefixIs(tmp, "! ")
524 && !contains(tmp, "(job aborted")) {
530 << "line: " << line << '\n'
531 << "Desc: " << desc << '\n'
532 << "Text: " << errstr << endl;
533 if (line == last_line)
539 if (line_count <= 5) {
540 terr.insertError(line, desc, errstr);
545 // information messages, TeX warnings and other
546 // warnings we have not caught earlier.
547 if (prefixIs(token, "Overfull ")) {
548 retval |= TEX_WARNING;
549 } else if (prefixIs(token, "Underfull ")) {
550 retval |= TEX_WARNING;
551 } else if (contains(token, "Rerun to get citations")) {
552 // Natbib seems to use this.
554 } else if (contains(token, "No pages of output")) {
555 // A dvi file was not created
557 } else if (contains(token, "That makes 100 errors")) {
558 // More than 100 errors were reprted
559 retval |= TOO_MANY_ERRORS;
563 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
568 void LaTeX::deplog(DepTable & head)
570 // This function reads the LaTeX log file end extracts all the external
571 // files used by the LaTeX run. The files are then entered into the
574 string logfile = ChangeExtension(file, ".log", true);
576 ifstream ifs(logfile.c_str());
578 // Now we read chars until we find a '('
585 // We now have c == '(', we now read the the sequence of
586 // chars until reaching EOL, ' ' or ')' and put that
590 if (c == '\n' || c == ' ' || c == ')')
594 lyxerr[Debug::DEPEND] << "Found file: "
595 << foundfile << endl;
597 // Ok now we found a file.
598 // Now we should make sure that
599 // this is a file that we can
600 // access through the normal
602 // (1) foundfile is an
603 // absolute path and should
605 if (AbsolutePath(foundfile)) {
606 lyxerr[Debug::DEPEND] << "AbsolutePath file: "
607 << foundfile << endl;
608 // On inital insert we want to do the update at once
609 // since this file can not be a file generated by
611 head.insert(foundfile, true);
615 // (2) foundfile is in the tmpdir
616 // insert it into head
617 if (FileInfo(OnlyFilename(foundfile)).exist()) {
618 if (suffixIs(foundfile, ".aux")) {
619 lyxerr[Debug::DEPEND] << "We don't want "
620 << OnlyFilename(foundfile)
621 << " in the dep file"
623 } else if (suffixIs(foundfile, ".tex")) {
624 // This is a tex file generated by LyX
625 // and latex is not likely to change this
627 lyxerr[Debug::DEPEND] << "Tmpdir TeX file: "
628 << OnlyFilename(foundfile)
630 head.insert(foundfile, true);
632 lyxerr[Debug::DEPEND] << "In tmpdir file:"
633 << OnlyFilename(foundfile)
635 head.insert(OnlyFilename(foundfile));
639 lyxerr[Debug::DEPEND]
640 << "Not a file or we are unable to find it."
646 void LaTeX::deptex(DepTable & head)
648 int except = AUX|LOG|DVI|BBL|IND|GLO;
651 for (int i = 0; i < file_count; ++i) {
652 if (!(all_files[i].file & except)) {
653 tmp = ChangeExtension(file,
654 all_files[i].extension,
656 lyxerr[Debug::DEPEND] << "deptex: " << tmp << endl;
657 if (fi.newFile(tmp).exist())