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"
38 // TODO: in no particular order
39 // - get rid of the extern BufferList and the call to
40 // BufferList::updateIncludedTeXfiles, this should either
41 // be done before calling LaTeX::funcs or in a completely
43 // - the bibtex command options should be supported.
44 // - the makeindex style files should be taken care of with
45 // the dependency mechanism.
46 // - makeindex commandline options should be supported
47 // - somewhere support viewing of bibtex and makeindex log files.
48 // - we should perhaps also scan the bibtex log file
49 // - we should perhaps also scan the bibtex log file
51 extern BufferList bufferlist;
53 struct texfile_struct {
54 LaTeX::TEX_FILES file;
55 char const * extension;
59 const texfile_struct all_files[] = {
60 { LaTeX::AUX, ".aux"},
61 { LaTeX::BBL, ".bbl"},
62 { LaTeX::DVI, ".dvi"},
63 { LaTeX::GLO, ".glo"},
64 { LaTeX::IDX, ".idx"},
65 { LaTeX::IND, ".ind"},
66 { LaTeX::LOF, ".lof"},
67 { LaTeX::LOA, ".loa"},
68 { LaTeX::LOG, ".log"},
69 { LaTeX::LOT, ".lot"},
70 { LaTeX::TOC, ".toc"},
71 { LaTeX::LTX, ".ltx"},
80 void TeXErrors::insertError(int line, string const & error_desc,
81 string const & error_text)
83 Error newerr(line, error_desc, error_text);
84 errors.push_back(newerr);
91 LaTeX::LaTeX(string const & latex, string const & f, string const & p)
92 : cmd(latex), file(f), path(p)
95 file_count = sizeof(all_files) / sizeof(texfile_struct);
97 depfile = file + ".dep";
101 int LaTeX::run(TeXErrors & terr, MiniBuffer * minib)
102 // We know that this function will only be run if the lyx buffer
103 // has been changed. We also know that a newly written .tex file
104 // is always different from the previous one because of the date
105 // in it. However it seems safe to run latex (at least) on time each
106 // time the .tex file changes.
108 int scanres = LaTeX::NO_ERRORS;
109 unsigned int count = 0; // number of times run
110 num_errors = 0; // just to make sure.
111 const unsigned int MAX_RUN = 6;
112 DepTable head; // empty head
113 bool rerun = false; // rerun requested
115 // The class LaTeX does not know the temp path.
116 bufferlist.updateIncludedTeXfiles(GetCWD());
118 // Never write the depfile if an error was encountered.
121 // first check if the file dependencies exist:
122 // ->If it does exist
123 // check if any of the files mentioned in it have
124 // changed (done using a checksum).
126 // run latex once and
127 // remake the dependency file
128 // -> if not changed:
129 // just return there is nothing to do for us.
130 // ->if it doesn't exist
132 // run latex once (we need to run latex once anyway) and
133 // remake the dependency file.
135 FileInfo fi(depfile);
136 bool run_bibtex = false;
138 // Read the dep file:
140 // Update the checksums
143 lyxerr[Debug::DEPEND] << "Dependency file exists" << endl;
144 if (head.sumchange()) {
146 lyxerr[Debug::DEPEND]
147 << "Dependency file has changed" << endl;
149 << "Run #" << count << endl;
150 minib->Set(string(_("LaTeX run number ")) + tostr(count));
153 scanres = scanLogFile(terr);
154 if (scanres & LaTeX::ERRORS) return scanres; // return on error
155 run_bibtex = scanAux(head);
157 lyxerr[Debug::DEPEND]
158 << "Bibtex demands rerun" << endl;
160 lyxerr[Debug::DEPEND] << "return no_change" << endl;
161 return LaTeX::NO_CHANGE;
165 lyxerr[Debug::DEPEND]
166 << "Dependency file does not exist" << endl;
168 << "Run #" << count << endl;
169 head.insert(file, true);
170 minib->Set(string(_("LaTeX run number ")) + tostr(count));
173 scanres = scanLogFile(terr);
174 if (scanres & LaTeX::ERRORS) return scanres; // return on error
177 // update the dependencies.
178 deplog(head); // reads the latex log
179 deptex(head); // checks for latex files
183 // At this point we must run external programs if needed.
184 // makeindex will be run if a .idx file changed or was generated.
185 // And if there were undefined citations or changes in references
186 // the .aux file is checked for signs of bibtex. Bibtex is then run
190 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
192 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
193 minib->Set(_("Running MakeIndex."));
195 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
199 if (scanres & LaTeX::UNDEF_CIT
200 || scanres & LaTeX::RERUN
202 // Here we must scan the .aux file and look for
203 // "\bibdata" and/or "\bibstyle". If one of those
204 // tags is found -> run bibtex and set rerun = true;
206 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
207 minib->Set(_("Running BibTeX."));
209 rerun = runBibTeX(ChangeExtension(file, ".aux", true), head);
213 // we know on this point that latex has been run once (or we just
214 // returned) and the question now is to decide if we need to run
215 // it any more. This is done by asking if any of the files in the
216 // dependency file has changed. (remember that the checksum for
217 // a given file is reported to have changed if it just was created)
218 // -> if changed or rerun == true:
219 // run latex once more and
220 // update the dependency structure
221 // -> if not changed:
222 // we does nothing at this point
224 if (rerun || head.sumchange()) {
227 lyxerr[Debug::DEPEND]
228 << "Dep. file has changed or rerun requested" << endl;
230 << "Run #" << count << endl;
231 minib->Set(string(_("LaTeX run number ")) + tostr(count));
234 scanres = scanLogFile(terr);
235 if (scanres & LaTeX::ERRORS) return scanres; // return on error
236 // update the depedencies
237 deplog(head); // reads the latex log
240 lyxerr[Debug::DEPEND] << "Dep. file has NOT changed" << endl;
244 // The inclusion of files generated by external programs like
245 // makeindex or bibtex might have done changes to pagenumbereing,
246 // etc. And because of this we must run the external programs
247 // again to make sure everything is redone correctly.
248 // Also there should be no need to run the external programs any
251 // run makeindex if the <file>.idx has changed or was generated.
252 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
254 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
255 minib->Set(_("Running MakeIndex."));
257 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
261 // we will only run latex more if the log file asks for it.
262 // or if the sumchange() is true.
263 // -> rerun asked for:
265 // remake the dependency file
266 // goto 2 or return if max runs are reached.
267 // -> rerun not asked for:
268 // just return (fall out of bottom of func)
270 while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN))
271 && count < MAX_RUN) {
272 // Yes rerun until message goes away, or until
273 // MAX_RUNS are reached.
276 lyxerr[Debug::LATEX] << "Run #" << count << endl;
277 minib->Set(string(_("LaTeX run number ")) + tostr(count));
280 scanres = scanLogFile(terr);
281 if (scanres & LaTeX::ERRORS) return scanres; // return on error
286 // Write the dependencies to file.
288 lyxerr[Debug::LATEX] << "Done." << endl;
293 int LaTeX::operator()()
296 string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null";
297 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
298 string tmp = cmd + ' ' + file + " > nul";
301 return one.startscript(Systemcalls::System, tmp);
305 bool LaTeX::runMakeIndex(string const & f)
307 lyxerr[Debug::LATEX] << "idx file has been made,"
308 " running makeindex on file "
311 // It should be possible to set the switches for makeindex
312 // sorting style and such. It would also be very convenient
313 // to be able to make style files from within LyX. This has
314 // to come for a later time. (0.13 perhaps?)
315 string tmp = "makeindex -c -q ";
318 one.startscript(Systemcalls::System, tmp);
323 bool LaTeX::scanAux(DepTable & dep)
325 // if any of the bib file has changed we don't have to
326 // check the .aux file.
327 if (dep.extchanged(".bib")
328 || dep.extchanged(".bst")) return true;
330 string aux = ChangeExtension(file, ".aux", true);
331 ifstream ifs(aux.c_str());
333 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
334 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
335 while (getline(ifs, token)) {
336 if (reg1.exact_match(token)) {
337 LRegex::SubMatches sub = reg1.exec(token);
338 string data = LSubstring(token, sub[1].first,
342 b = data.find_first_of(',', 0);
344 if (b == string::npos)
347 l = data.substr( 0, b - 0);
348 data.erase(0, b + 1);
352 ChangeExtension(l, "bib", false), "bib");
353 if (!full_l.empty()) {
354 if (!dep.exist(full_l))
357 } while (b != string::npos);
358 } else if (reg2.exact_match(token)) {
359 LRegex::SubMatches sub = reg2.exec(token);
360 string style = LSubstring(token, sub[1].first,
362 // token is now the style file
363 // pass it to the helper
366 ChangeExtension(style, "bst", false),
368 if (!full_l.empty()) {
369 if (!dep.exist(full_l))
378 bool LaTeX::runBibTeX(string const & f, DepTable & dep)
380 // Since a run of Bibtex mandates more latex runs it is ok to
381 // remove all ".bib" and ".bst" files, it is also required to
382 // discover style and database changes.
383 dep.remove_files_with_extension(".bib");
384 dep.remove_files_with_extension(".bst");
385 ifstream ifs(f.c_str());
387 bool using_bibtex = false;
388 LRegex reg1("\\\\bibdata\\{([^}]+)\\}");
389 LRegex reg2("\\\\bibstyle\\{([^}]+)\\}");
390 while (getline(ifs, token)) {
391 if (reg1.exact_match(token)) {
393 LRegex::SubMatches const & sub = reg1.exec(token);
394 string data = LSubstring(token, sub[1].first,
396 // data is now all the bib files separated by ','
397 // get them one by one and pass them to the helper
400 b = data.find_first_of(',', 0);
402 if (b == string::npos)
405 l = data.substr(0, b - 0);
406 data.erase(0, b + 1);
410 ChangeExtension(l, "bib", false),
412 lyxerr[Debug::LATEX] << "Bibtex database: `"
413 << full_l << "'" << endl;
414 if (!full_l.empty()) {
415 // add full_l to the dep file.
416 dep.insert(full_l, true);
418 } while (b != string::npos);
419 } else if (reg2.exact_match(token)) {
421 LRegex::SubMatches const & sub = reg2.exec(token);
422 string style = LSubstring(token, sub[1].first,
424 // token is now the style file
425 // pass it to the helper
428 ChangeExtension(style, "bst", false),
430 lyxerr[Debug::LATEX] << "Bibtex style: `"
431 << full_l << "'" << endl;
432 if (!full_l.empty()) {
433 // add full_l to the dep file.
434 dep.insert(full_l, true);
440 string tmp = "bibtex ";
441 tmp += ChangeExtension(file, string(), true);
443 one.startscript(Systemcalls::System, tmp);
446 // bibtex was not run.
451 int LaTeX::scanLogFile(TeXErrors & terr)
455 int retval = NO_ERRORS;
456 string tmp = ChangeExtension(file, ".log", true);
457 lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
458 ifstream ifs(tmp.c_str());
461 while (getline(ifs, token)) {
462 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
467 if (prefixIs(token, "LaTeX Warning:")) {
468 // Here shall we handle different
470 retval |= LATEX_WARNING;
471 lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
472 if (contains(token, "Rerun to get cross-references")) {
475 << "We should rerun." << endl;
476 } else if (contains(token, "Citation")
477 && contains(token, "on page")
478 && contains(token, "undefined")) {
481 } else if (prefixIs(token, "Package")) {
483 retval |= PACKAGE_WARNING;
484 if (contains(token, "natbib Warning:")) {
486 if (contains(token, "Citation")
487 && contains(token, "on page")
488 && contains(token, "undefined")) {
491 } else if (contains(token, "Rerun LaTeX.")) {
492 // at least longtable.sty might use this.
495 } else if (prefixIs(token, "! ")) {
496 // Ok, we have something that looks like a TeX Error
497 // but what do we really have.
499 // Just get the error description:
500 string desc(token, 2);
501 if (contains(token, "LaTeX Error:"))
502 retval |= LATEX_ERROR;
507 if (!getline(ifs, tmp))
511 } while (!prefixIs(tmp, "l."));
512 if (prefixIs(tmp, "l.")) {
513 // we have a latex error
515 // get the line number:
517 sscanf(tmp.c_str(), "l.%d", &line);
518 // get the rest of the message:
519 string errstr(tmp, tmp.find(' '));
522 while (!contains(errstr, "l.")
524 && !prefixIs(tmp, "! ")
525 && !contains(tmp, "(job aborted")) {
531 << "line: " << line << '\n'
532 << "Desc: " << desc << '\n'
533 << "Text: " << errstr << endl;
534 if (line == last_line)
540 if (line_count <= 5) {
541 terr.insertError(line, desc, errstr);
546 // information messages, TeX warnings and other
547 // warnings we have not caught earlier.
548 if (prefixIs(token, "Overfull ")) {
549 retval |= TEX_WARNING;
550 } else if (prefixIs(token, "Underfull ")) {
551 retval |= TEX_WARNING;
552 } else if (contains(token, "Rerun to get citations")) {
553 // Natbib seems to use this.
555 } else if (contains(token, "No pages of output")) {
556 // A dvi file was not created
558 } else if (contains(token, "That makes 100 errors")) {
559 // More than 100 errors were reprted
560 retval |= TOO_MANY_ERRORS;
564 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
569 void LaTeX::deplog(DepTable & head)
571 // This function reads the LaTeX log file end extracts all the external
572 // files used by the LaTeX run. The files are then entered into the
575 string logfile = ChangeExtension(file, ".log", true);
577 ifstream ifs(logfile.c_str());
579 // Now we read chars until we find a '('
586 // We now have c == '(', we now read the the sequence of
587 // chars until reaching EOL, ' ' or ')' and put that
591 if (c == '\n' || c == ' ' || c == ')')
595 lyxerr[Debug::DEPEND] << "Found file: "
596 << foundfile << endl;
598 // Ok now we found a file.
599 // Now we should make sure that
600 // this is a file that we can
601 // access through the normal
603 // (1) foundfile is an
604 // absolute path and should
606 if (AbsolutePath(foundfile)) {
607 lyxerr[Debug::DEPEND] << "AbsolutePath file: "
608 << foundfile << endl;
609 // On inital insert we want to do the update at once
610 // since this file can not be a file generated by
612 head.insert(foundfile, true);
616 // (2) foundfile is in the tmpdir
617 // insert it into head
618 if (FileInfo(OnlyFilename(foundfile)).exist()) {
619 if (suffixIs(foundfile, ".aux")) {
620 lyxerr[Debug::DEPEND] << "We don't want "
621 << OnlyFilename(foundfile)
622 << " in the dep file"
624 } else if (suffixIs(foundfile, ".tex")) {
625 // This is a tex file generated by LyX
626 // and latex is not likely to change this
628 lyxerr[Debug::DEPEND] << "Tmpdir TeX file: "
629 << OnlyFilename(foundfile)
631 head.insert(foundfile, true);
633 lyxerr[Debug::DEPEND] << "In tmpdir file:"
634 << OnlyFilename(foundfile)
636 head.insert(OnlyFilename(foundfile));
640 lyxerr[Debug::DEPEND]
641 << "Not a file or we are unable to find it."
647 void LaTeX::deptex(DepTable & head)
649 int except = AUX|LOG|DVI|BBL|IND|GLO;
652 for (int i = 0; i < file_count; ++i) {
653 if (!(all_files[i].file & except)) {
654 tmp = ChangeExtension(file,
655 all_files[i].extension,
657 lyxerr[Debug::DEPEND] << "deptex: " << tmp << endl;
658 if (fi.newFile(tmp).exist())