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::LATEX] << "Dependency file exists" << endl;
143 if (head.sumchange()) {
146 << "Dependency file has changed\n"
147 << "Run #" << count << endl;
148 minib->Set(string(_("LaTeX run number ")) + tostr(count));
151 scanres = scanLogFile(terr);
152 if (scanres & LaTeX::ERRORS) return scanres; // return on error
153 run_bibtex = scanAux(head);
155 lyxerr << "Bibtex demands rerun" << endl;
157 lyxerr[Debug::LATEX] << "return no_change" << endl;
158 return LaTeX::NO_CHANGE;
162 lyxerr[Debug::LATEX] << "Dependency file does not exist\n"
163 << "Run #" << count << endl;
164 head.insert(file, true);
165 minib->Set(string(_("LaTeX run number ")) + tostr(count));
168 scanres = scanLogFile(terr);
169 if (scanres & LaTeX::ERRORS) return scanres; // return on error
172 // update the dependencies.
173 deplog(head); // reads the latex log
174 deptex(head); // checks for latex files
178 // At this point we must run external programs if needed.
179 // makeindex will be run if a .idx file changed or was generated.
180 // And if there were undefined citations or changes in references
181 // the .aux file is checked for signs of bibtex. Bibtex is then run
185 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
187 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
188 minib->Set(_("Running MakeIndex."));
190 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
194 if (scanres & LaTeX::UNDEF_CIT
195 || scanres & LaTeX::RERUN
197 // Here we must scan the .aux file and look for
198 // "\bibdata" and/or "\bibstyle". If one of those
199 // tags is found -> run bibtex and set rerun = true;
201 lyxerr[Debug::LATEX] << "Running BibTeX." << endl;
202 minib->Set(_("Running BibTeX."));
204 rerun = runBibTeX(ChangeExtension(file, ".aux", true), head);
208 // we know on this point that latex has been run once (or we just
209 // returned) and the question now is to decide if we need to run
210 // it any more. This is done by asking if any of the files in the
211 // dependency file has changed. (remember that the checksum for
212 // a given file is reported to have changed if it just was created)
213 // -> if changed or rerun == true:
214 // run latex once more and
215 // update the dependency structure
216 // -> if not changed:
217 // we does nothing at this point
219 if (rerun || head.sumchange()) {
223 << "Dep. file has changed or rerun requested\n"
224 << "Run #" << count << endl;
225 minib->Set(string(_("LaTeX run number ")) + tostr(count));
228 scanres = scanLogFile(terr);
229 if (scanres & LaTeX::ERRORS) return scanres; // return on error
230 // update the depedencies
231 deplog(head); // reads the latex log
234 lyxerr[Debug::LATEX] << "Dep. file has NOT changed" << endl;
238 // The inclusion of files generated by external programs like
239 // makeindex or bibtex might have done changes to pagenumbereing,
240 // etc. And because of this we must run the external programs
241 // again to make sure everything is redone correctly.
242 // Also there should be no need to run the external programs any
245 // run makeindex if the <file>.idx has changed or was generated.
246 if (head.haschanged(ChangeExtension(file, ".idx", true))) {
248 lyxerr[Debug::LATEX] << "Running MakeIndex." << endl;
249 minib->Set(_("Running MakeIndex."));
251 rerun = runMakeIndex(ChangeExtension(file, ".idx", true));
255 // we will only run latex more if the log file asks for it.
256 // or if the sumchange() is true.
257 // -> rerun asked for:
259 // remake the dependency file
260 // goto 2 or return if max runs are reached.
261 // -> rerun not asked for:
262 // just return (fall out of bottom of func)
264 while ((head.sumchange() || rerun || (scanres & LaTeX::RERUN))
265 && count < MAX_RUN) {
266 // Yes rerun until message goes away, or until
267 // MAX_RUNS are reached.
270 lyxerr[Debug::LATEX] << "Run #" << count << endl;
271 minib->Set(string(_("LaTeX run number ")) + tostr(count));
274 scanres = scanLogFile(terr);
275 if (scanres & LaTeX::ERRORS) return scanres; // return on error
280 // Write the dependencies to file.
282 lyxerr[Debug::LATEX] << "Done." << endl;
287 int LaTeX::operator()()
290 string tmp = cmd + ' ' + file + " > /dev/null";
291 #else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null"
292 string tmp = cmd + ' ' + file + " > nul";
295 return one.startscript(Systemcalls::System, tmp);
299 bool LaTeX::runMakeIndex(string const & f)
301 lyxerr[Debug::LATEX] << "idx file has been made,"
302 " running makeindex on file "
305 // It should be possible to set the switches for makeindex
306 // sorting style and such. It would also be very convenient
307 // to be able to make style files from within LyX. This has
308 // to come for a later time. (0.13 perhaps?)
309 string tmp = "makeindex -c -q ";
312 one.startscript(Systemcalls::System, tmp);
317 bool LaTeX::scanAux(DepTable & dep)
319 // if any of the bib file has changed we don't have to
320 // check the .aux file.
321 if (dep.extchanged(".bib")
322 || dep.extchanged(".bst")) return true;
324 string aux = ChangeExtension(file, ".aux", true);
325 ifstream ifs(aux.c_str());
327 LRegex reg1("\\\\bibdata{([^}]+)}");
328 LRegex reg2("\\\\bibstyle{([^}]+)}");
329 while (getline(ifs, token)) {
330 if (reg1.exact_match(token)) {
331 LRegex::SubMatches sub = reg1.exec(token);
332 string data = LSubstring(token, sub[1].first,
336 b = data.find_first_of(',', 0);
338 if (b == string::npos)
341 l = data.substr( 0, b - 0);
342 data.erase(0, b + 1);
346 ChangeExtension(l, "bib", false), "bib");
347 if (!full_l.empty()) {
348 if (!dep.exist(full_l))
351 } while (b != string::npos);
352 } else if (reg2.exact_match(token)) {
353 LRegex::SubMatches sub = reg2.exec(token);
354 string style = LSubstring(token, sub[1].first,
356 // token is now the style file
357 // pass it to the helper
360 ChangeExtension(style, "bst", false),
362 if (!full_l.empty()) {
363 if (!dep.exist(full_l))
372 bool LaTeX::runBibTeX(string const & f, DepTable & dep)
374 // Since a run of Bibtex mandates more latex runs it is ok to
375 // remove all ".bib" and ".bst" files, it is also required to
376 // discover style and database changes.
377 dep.remove_files_with_extension(".bib");
378 dep.remove_files_with_extension(".bst");
379 ifstream ifs(f.c_str());
381 bool using_bibtex = false;
382 LRegex reg1("\\\\bibdata{([^}]+)}");
383 LRegex reg2("\\\\bibstyle{([^}]+)}");
384 while (getline(ifs, token)) {
385 if (reg1.exact_match(token)) {
387 LRegex::SubMatches const & sub = reg1.exec(token);
388 string data = LSubstring(token, sub[1].first,
390 // data is now all the bib files separated by ','
391 // get them one by one and pass them to the helper
394 b = data.find_first_of(',', 0);
396 if (b == string::npos)
399 l = data.substr(0, b - 0);
400 data.erase(0, b + 1);
404 ChangeExtension(l, "bib", false),
406 lyxerr[Debug::LATEX] << "Bibtex database: `"
407 << full_l << "'" << endl;
408 if (!full_l.empty()) {
409 // add full_l to the dep file.
410 dep.insert(full_l, true);
412 } while (b != string::npos);
413 } else if (reg2.exact_match(token)) {
415 LRegex::SubMatches const & sub = reg2.exec(token);
416 string style = LSubstring(token, sub[1].first,
418 // token is now the style file
419 // pass it to the helper
422 ChangeExtension(style, "bst", false),
424 lyxerr[Debug::LATEX] << "Bibtex style: `"
425 << full_l << "'" << endl;
426 if (!full_l.empty()) {
427 // add full_l to the dep file.
428 dep.insert(full_l, true);
434 string tmp = "bibtex ";
435 tmp += ChangeExtension(file, string(), true);
437 one.startscript(Systemcalls::System, tmp);
440 // bibtex was not run.
445 int LaTeX::scanLogFile(TeXErrors & terr)
447 int retval = NO_ERRORS;
448 string tmp = ChangeExtension(file, ".log", true);
449 lyxerr[Debug::LATEX] << "Log file: " << tmp << endl;
450 ifstream ifs(tmp.c_str());
453 while (getline(ifs, token)) {
454 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
459 if (prefixIs(token, "LaTeX Warning:")) {
460 // Here shall we handle different
462 retval |= LATEX_WARNING;
463 lyxerr[Debug::LATEX] << "LaTeX Warning." << endl;
464 if (contains(token, "Rerun to get cross-references")) {
467 << "We should rerun." << endl;
468 } else if (contains(token, "Citation")
469 && contains(token, "on page")
470 && contains(token, "undefined")) {
473 } else if (prefixIs(token, "Package")) {
475 retval |= PACKAGE_WARNING;
476 if (contains(token, "natbib Warning:")) {
478 if (contains(token, "Citation")
479 && contains(token, "on page")
480 && contains(token, "undefined")) {
483 } else if (contains(token, "Rerun LaTeX.")) {
484 // at least longtable.sty might use this.
487 } else if (prefixIs(token, "! ")) {
488 // Ok, we have something that looks like a TeX Error
489 // but what do we really have.
491 // Just get the error description:
492 string desc(token, 2);
493 if (contains(token, "LaTeX Error:"))
494 retval |= LATEX_ERROR;
498 if (prefixIs(tmp, "l.")) {
499 // we have a latex error
501 // get the line number:
503 sscanf(tmp.c_str(), "l.%d", &line);
504 // get the rest of the message:
505 string errstr(tmp, tmp.find(' '));
508 while (!contains(errstr, "l.")
510 && !prefixIs(tmp, "! ")
511 && !contains(tmp, "(job aborted")) {
517 << "line: " << line << '\n'
518 << "Desc: " << desc << '\n'
519 << "Text: " << errstr << endl;
520 terr.insertError(line, desc, errstr);
524 // information messages, TeX warnings and other
525 // warnings we have not caught earlier.
526 if (prefixIs(token, "Overfull ")) {
527 retval |= TEX_WARNING;
528 } else if (prefixIs(token, "Underfull ")) {
529 retval |= TEX_WARNING;
530 } else if (contains(token, "Rerun to get citations")) {
531 // Natbib seems to use this.
533 } else if (contains(token, "No pages of output")) {
534 // A dvi file was not created
536 } else if (contains(token, "That makes 100 errors")) {
537 // More than 100 errors were reprted
538 retval |= TOO_MANY_ERRORS;
542 lyxerr[Debug::LATEX] << "Log line: " << token << endl;
547 void LaTeX::deplog(DepTable & head)
549 // This function reads the LaTeX log file end extracts all the external
550 // files used by the LaTeX run. The files are then entered into the
553 string logfile = ChangeExtension(file, ".log", true);
555 ifstream ifs(logfile.c_str());
557 // Now we read chars until we find a '('
564 // We now have c == '(', we now read the the sequence of
565 // chars until reaching EOL, ' ' or ')' and put that
569 if (c == '\n' || c == ' ' || c == ')')
573 lyxerr[Debug::LATEX] << "Found file: "
574 << foundfile << endl;
576 // Ok now we found a file.
577 // Now we should make sure that
578 // this is a file that we can
579 // access through the normal
581 // (1) foundfile is an
582 // absolute path and should
584 if (AbsolutePath(foundfile)) {
585 lyxerr[Debug::LATEX] << "AbsolutePath file: "
586 << foundfile << endl;
587 // On inital insert we want to do the update at once
588 // since this file can not be a file generated by
590 head.insert(foundfile, true);
594 // (2) foundfile is in the tmpdir
595 // insert it into head
596 if (FileInfo(OnlyFilename(foundfile)).exist()) {
597 if (suffixIs(foundfile, ".aux")) {
598 lyxerr[Debug::LATEX] << "We don't want "
599 << OnlyFilename(foundfile)
600 << " in the dep file"
602 } else if (suffixIs(foundfile, ".tex")) {
603 // This is a tex file generated by LyX
604 // and latex is not likely to change this
606 lyxerr[Debug::LATEX] << "Tmpdir TeX file: "
607 << OnlyFilename(foundfile)
609 head.insert(foundfile, true);
611 lyxerr[Debug::LATEX] << "In tmpdir file:"
612 << OnlyFilename(foundfile)
614 head.insert(OnlyFilename(foundfile));
619 << "Not a file or we are unable to find it."
625 void LaTeX::deptex(DepTable & head)
627 int except = AUX|LOG|DVI|BBL|IND|GLO;
630 for (int i = 0; i < file_count; ++i) {
631 if (!(all_files[i].file & except)) {
632 tmp = ChangeExtension(file,
633 all_files[i].extension,
635 lyxerr[Debug::LATEX] << "deptex: " << tmp << endl;
636 if (fi.newFile(tmp).exist())