3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
17 #include "BufferParams.h"
21 #include "WordLangTuple.h"
23 #include "support/debug.h"
24 #include "support/gettext.h"
25 #include "support/ForkedCalls.h"
26 #include "support/lstrings.h"
27 #include "support/unicode.h"
29 // HP-UX 11.x doesn't have this header
30 #ifdef HAVE_SYS_SELECT_H
31 # include <sys/select.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
37 using boost::shared_ptr;
43 using support::bformat;
47 class LaunchIspell : public support::ForkedProcess {
48 typedef support::ForkedProcess ForkedProcess;
51 LaunchIspell(BufferParams const & p, string const & l,
52 int * in, int * out, int * err)
53 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
55 virtual shared_ptr<ForkedProcess> clone() const {
56 return shared_ptr<ForkedProcess>(new LaunchIspell(*this));
62 virtual int generateChild();
65 BufferParams const & params;
73 int LaunchIspell::start()
75 command_ = lyxrc.isp_command;
80 int LaunchIspell::generateChild()
82 pid_t isp_pid = fork();
85 // failed (-1) or parent process (>0)
90 dup2(pipein[0], STDIN_FILENO);
91 dup2(pipeout[1], STDOUT_FILENO);
92 dup2(pipeerr[1], STDERR_FILENO);
103 char * tmp = new char[lyxrc.isp_command.length() + 1];
104 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
105 tmp[lyxrc.isp_command.length()] = '\0';
108 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
111 if (lang != "default") {
113 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
115 tmp = new char[lang.length() + 1];
116 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
120 if (lyxrc.isp_accept_compound) {
121 // Consider run-together words as legal compounds
123 string("-C").copy(tmp, 2); tmp[2] = '\0';
126 // Report run-together words with
127 // missing blanks as errors
129 string("-B").copy(tmp, 2); tmp[2] = '\0';
132 if (lyxrc.isp_use_esc_chars) {
133 // Specify additional characters that
134 // can be part of a word
136 string("-w").copy(tmp, 2); tmp[2] = '\0';
138 // Put the escape chars in ""s
139 string tms = '"' + lyxrc.isp_esc_chars + '"';
140 tmp = new char[tms.length() + 1];
141 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
144 if (lyxrc.isp_use_pers_dict) {
145 // Specify an alternate personal dictionary
147 string("-p").copy(tmp, 2);
150 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
151 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
152 tmp[lyxrc.isp_pers_dict.length()] = '\0';
155 if (lyxrc.isp_use_input_encoding &&
156 params.inputenc != "default") {
157 string enc = (params.inputenc == "auto")
158 ? params.language->encoding()->latexName()
160 size_t const n = enc.length();
162 string("-T").copy(tmp, 2);
164 argv[argc++] = tmp; // Input encoding
165 tmp = new char[n + 1];
173 execvp(argv[0], const_cast<char * const *>(argv));
175 // free the memory used by string::copy in the
177 for (int i = 0; i < argc - 1; ++i)
180 lyxerr << "LyX: Failed to start ispell!" << endl;
185 string const to_iconv_encoding(docstring const & s, string const & encoding)
187 if (lyxrc.isp_use_input_encoding) {
188 std::vector<char> const encoded =
189 ucs4_to_eightbit(s.data(), s.length(), encoding);
190 return string(encoded.begin(), encoded.end());
192 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
197 docstring const from_iconv_encoding(string const & s, string const & encoding)
199 if (lyxrc.isp_use_input_encoding) {
200 std::vector<char_type> const ucs4 =
201 eightbit_to_ucs4(s.data(), s.length(), encoding);
202 return docstring(ucs4.begin(), ucs4.end());
204 // FIXME UNICODE: s is not in UTF8, but probably the locale encoding
211 ISpell::ISpell(BufferParams const & params, string const & lang)
212 : in(0), out(0), inerr(0), str(0)
214 //LYXERR(Debug::GUI, "Created ispell");
216 encoding = params.encoding().iconvName();
218 // static due to the setvbuf. Ugly.
219 static char o_buf[BUFSIZ];
221 // We need to throw an exception not do this
222 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
223 = pipeerr[0] = pipeerr[1] = -1;
225 // This is what happens when goto gets banned.
227 if (pipe(pipein) == -1) {
228 error_ = _("Can't create pipe for spellchecker.");
232 if (pipe(pipeout) == -1) {
235 error_ = _("Can't create pipe for spellchecker.");
239 if (pipe(pipeerr) == -1) {
244 error_ = _("Can't create pipe for spellchecker.");
248 if ((out = fdopen(pipein[1], "w")) == 0) {
249 error_ = _("Can't open pipe for spellchecker.");
253 if ((in = fdopen(pipeout[0], "r")) == 0) {
254 error_ = _("Can't open pipe for spellchecker.");
258 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
259 error_ = _("Can't open pipe for spellchecker.");
263 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
265 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
267 if (li->start() == -1) {
268 error_ = _("Could not create an ispell process.\nYou may not have "
269 "the right languages installed.");
274 // Parent process: Read ispells identification message
277 bool error = select(err_read);
281 // Set terse mode (silently accept correct words)
286 // must have read something from stderr
287 // FIXME UNICODE: buf is not in UTF8, but probably the locale encoding
288 error_ = from_utf8(buf);
290 // select returned error
291 error_ = _("The ispell process returned an error.\nPerhaps "
292 "it has been configured wrongly ?");
308 //LYXERR(Debug::GUI, "Killing ispell");
317 fputs("#\n", out); // Save personal dictionary
333 bool ISpell::select(bool & err_read)
339 FD_SET(pipeout[0], &infds);
340 FD_SET(pipeerr[0], &infds);
344 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
345 SELECT_TYPE_ARG234 (&infds),
348 SELECT_TYPE_ARG5 (&tv));
354 if (FD_ISSET(pipeerr[0], &infds)) {
355 fgets(buf, BUFSIZ, inerr);
360 fgets(buf, BUFSIZ, in);
366 docstring const ISpell::nextMiss()
368 // Well, somebody is a sick fuck.
370 if (str == 0 || *(e+1) == '\0')
373 e = strpbrk(b, ",\n");
376 return from_iconv_encoding(b, encoding);
383 return child_.get() && child_->running();
387 enum ISpell::Result ISpell::check(WordLangTuple const & word)
389 // FIXME Please rewrite to use string.
393 string const encoded = to_iconv_encoding(word.word(), encoding);
394 if (encoded.empty()) {
396 _("Could not check word `%1$s' because it could not be converted to encoding `%2$s'."),
397 word.word(), from_ascii(encoding));
400 ::fputs(encoded.c_str(), out);
404 bool error = select(err_read);
407 error_ = _("Could not communicate with the ispell spellchecker process.");
412 // FIXME UNICODE: buf is not in UTF8, but probably the locale encoding
413 error_ = from_utf8(buf);
417 // I think we have to check if ispell is still alive here because
418 // the signal-handler could have disabled blocking on the fd
435 case '#': // Not found, no near misses and guesses
438 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
439 case '&': // Not found, but we have near misses
441 res = SUGGESTED_WORDS;
442 char * p = strpbrk(buf, ":");
443 str = new char[strlen(p) + 1];
448 default: // This shouldn't happen, but you know Murphy
453 if (res != IGNORED_WORD) {
454 /* wait for ispell to finish */
462 void ISpell::insert(WordLangTuple const & word)
464 string const encoded = to_iconv_encoding(word.word(), encoding);
465 if (encoded.empty()) {
467 _("Could not insert word `%1$s' because it could not be converted to encoding `%2$s'."),
468 word.word(), from_ascii(encoding));
471 ::fputc('*', out); // Insert word in personal dictionary
472 ::fputs(encoded.c_str(), out);
477 void ISpell::accept(WordLangTuple const & word)
479 string const encoded = to_iconv_encoding(word.word(), encoding);
480 if (encoded.empty()) {
482 _("Could not accept word `%1$s' because it could not be converted to encoding `%2$s'."),
483 word.word(), from_ascii(encoding));
486 ::fputc('@', out); // Accept in this session
487 ::fputs(encoded.c_str(), out);
492 docstring const ISpell::error()