1 /**
2 * \file Chktex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
5 *
6 * \author Asger Alstrup
7 *
8 * Full author contact details are available in file CREDITS.
9 */
10
11 #include <config.h>
12
13 #include "Chktex.h"
14
15 #include "LaTeX.h" // TeXErrors
16
17 #include "support/convert.h"
18 #include "support/docstream.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/Systemcall.h"
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29
Chktex(string const & chktex,string const & f,string const & p)30 Chktex::Chktex(string const & chktex, string const & f, string const & p)
31 : cmd(chktex), file(f), path(p)
32 {}
33
34
run(TeXErrors & terr)35 int Chktex::run(TeXErrors &terr)
36 {
37 // run chktex
38 string log = onlyFileName(changeExtension(file, ".log"));
39 string tmp = cmd + " -q -v0 -b0 -x " + file + " -o " + log;
40 Systemcall one;
41 int result = one.startscript(Systemcall::Wait, tmp);
42 // ChkTeX (as of v. 1.7.7) has the following return values:
43 // 0 = EXIT_SUCCESS : program ran successfully, nothing to report
44 // 1 = EXIT_FAILURE : program ran unsucessfully
45 // 2 = EXIT_WARNINGS : program ran successfully, only warnings to report
46 // 3 = EXIT_ERRORS : program ran successfully, errors to report
47 // We only check for EXIT_FAILURE here, since older versions of ChkTeX
48 // returned 0 also in case 2 and 3.
49 if (result == EXIT_FAILURE)
50 result = -1;
51 else
52 result = scanLogFile(terr);
53 return result;
54 }
55
56
scanLogFile(TeXErrors & terr)57 int Chktex::scanLogFile(TeXErrors & terr)
58 {
59 int retval = 0;
60
61 // FIXME: Find out whether onlyFileName() is really needed,
62 // or whether makeAbsPath(onlyFileName()) is a noop here
63 FileName const tmp(makeAbsPath(onlyFileName(changeExtension(file, ".log"))));
64
65 docstring const msg(_("ChkTeX warning id # %1$s"));
66 docstring token;
67 // FIXME UNICODE
68 // We have no idea what the encoding of the error file is
69 ifdocstream ifs(tmp.toFilesystemEncoding().c_str());
70 while (getline(ifs, token)) {
71 docstring srcfile;
72 docstring line;
73 docstring pos;
74 docstring warno;
75 docstring warning;
76 token = split(token, srcfile, ':');
77 token = split(token, line, ':');
78 token = split(token, pos, ':');
79 token = split(token, warno, ':');
80 token = split(token, warning, ':');
81
82 int const lineno = convert<int>(line);
83
84 terr.insertError(lineno, bformat(msg, warno), warning);
85
86 ++retval;
87 }
88 return retval;
89 }
90
91
92 } // namespace lyx
93