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"
23 #include "WordLangTuple.h"
25 #include "support/forkedcall.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>
40 using support::bformat;
42 using boost::shared_ptr;
44 #ifndef CXX_GLOBAL_CSTD
57 class LaunchIspell : public support::ForkedProcess {
58 typedef support::ForkedProcess ForkedProcess;
61 LaunchIspell(BufferParams const & p, string const & l,
62 int * in, int * out, int * err)
63 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
65 virtual shared_ptr<ForkedProcess> clone() const {
66 return shared_ptr<ForkedProcess>(new LaunchIspell(*this));
72 virtual int generateChild();
75 BufferParams const & params;
83 int LaunchIspell::start()
85 command_ = lyxrc.isp_command;
90 int LaunchIspell::generateChild()
92 pid_t isp_pid = fork();
95 // failed (-1) or parent process (>0)
100 dup2(pipein[0], STDIN_FILENO);
101 dup2(pipeout[1], STDOUT_FILENO);
102 dup2(pipeerr[1], STDERR_FILENO);
113 char * tmp = new char[lyxrc.isp_command.length() + 1];
114 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
115 tmp[lyxrc.isp_command.length()] = '\0';
118 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
121 if (lang != "default") {
123 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
125 tmp = new char[lang.length() + 1];
126 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
130 if (lyxrc.isp_accept_compound) {
131 // Consider run-together words as legal compounds
133 string("-C").copy(tmp, 2); tmp[2] = '\0';
136 // Report run-together words with
137 // missing blanks as errors
139 string("-B").copy(tmp, 2); tmp[2] = '\0';
142 if (lyxrc.isp_use_esc_chars) {
143 // Specify additional characters that
144 // can be part of a word
146 string("-w").copy(tmp, 2); tmp[2] = '\0';
148 // Put the escape chars in ""s
149 string tms = '"' + lyxrc.isp_esc_chars + '"';
150 tmp = new char[tms.length() + 1];
151 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
154 if (lyxrc.isp_use_pers_dict) {
155 // Specify an alternate personal dictionary
157 string("-p").copy(tmp, 2);
160 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
161 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
162 tmp[lyxrc.isp_pers_dict.length()] = '\0';
165 if (lyxrc.isp_use_input_encoding &&
166 params.inputenc != "default") {
167 string enc = (params.inputenc == "auto")
168 ? params.language->encoding()->latexName()
170 size_t const n = enc.length();
172 string("-T").copy(tmp, 2);
174 argv[argc++] = tmp; // Input encoding
175 tmp = new char[n + 1];
183 execvp(argv[0], const_cast<char * const *>(argv));
185 // free the memory used by string::copy in the
187 for (int i = 0; i < argc - 1; ++i)
190 lyxerr << "LyX: Failed to start ispell!" << endl;
195 string const to_iconv_encoding(docstring const & s, string const & encoding)
197 if (lyxrc.isp_use_input_encoding) {
198 std::vector<char> const encoded =
199 ucs4_to_eightbit(s.data(), s.length(), encoding);
200 return string(encoded.begin(), encoded.end());
202 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
207 docstring const from_iconv_encoding(string const & s, string const & encoding)
209 if (lyxrc.isp_use_input_encoding) {
210 std::vector<char_type> const ucs4 =
211 eightbit_to_ucs4(s.data(), s.length(), encoding);
212 return docstring(ucs4.begin(), ucs4.end());
214 // FIXME UNICODE: s is not in UTF8, but probably the locale encoding
221 ISpell::ISpell(BufferParams const & params, string const & lang)
222 : in(0), out(0), inerr(0), str(0)
224 LYXERR(Debug::GUI) << "Created ispell" << endl;
226 encoding = params.encoding().iconvName();
228 // static due to the setvbuf. Ugly.
229 static char o_buf[BUFSIZ];
231 // We need to throw an exception not do this
232 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
233 = pipeerr[0] = pipeerr[1] = -1;
235 // This is what happens when goto gets banned.
237 if (pipe(pipein) == -1) {
238 error_ = _("Can't create pipe for spellchecker.");
242 if (pipe(pipeout) == -1) {
245 error_ = _("Can't create pipe for spellchecker.");
249 if (pipe(pipeerr) == -1) {
254 error_ = _("Can't create pipe for spellchecker.");
258 if ((out = fdopen(pipein[1], "w")) == 0) {
259 error_ = _("Can't open pipe for spellchecker.");
263 if ((in = fdopen(pipeout[0], "r")) == 0) {
264 error_ = _("Can't open pipe for spellchecker.");
268 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
269 error_ = _("Can't open pipe for spellchecker.");
273 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
275 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
277 if (li->start() == -1) {
278 error_ = _("Could not create an ispell process.\nYou may not have "
279 "the right languages installed.");
284 // Parent process: Read ispells identification message
287 bool error = select(err_read);
291 // Set terse mode (silently accept correct words)
296 // must have read something from stderr
297 // FIXME UNICODE: buf is not in UTF8, but probably the locale encoding
298 error_ = from_utf8(buf);
300 // select returned error
301 error_ = _("The ispell process returned an error.\nPerhaps "
302 "it has been configured wrongly ?");
318 LYXERR(Debug::GUI) << "Killing ispell" << endl;
327 fputs("#\n", out); // Save personal dictionary
343 bool ISpell::select(bool & err_read)
349 FD_SET(pipeout[0], &infds);
350 FD_SET(pipeerr[0], &infds);
354 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
355 SELECT_TYPE_ARG234 (&infds),
358 SELECT_TYPE_ARG5 (&tv));
364 if (FD_ISSET(pipeerr[0], &infds)) {
365 fgets(buf, BUFSIZ, inerr);
370 fgets(buf, BUFSIZ, in);
376 docstring const ISpell::nextMiss()
378 // Well, somebody is a sick fuck.
380 if (str == 0 || *(e+1) == '\0')
383 e = strpbrk(b, ",\n");
386 return from_iconv_encoding(b, encoding);
393 return child_.get() && child_->running();
397 enum ISpell::Result ISpell::check(WordLangTuple const & word)
399 // FIXME Please rewrite to use string.
403 string const encoded = to_iconv_encoding(word.word(), encoding);
404 if (encoded.empty()) {
406 _("Could not check word `%1$s' because it could not be converted to encoding `%2$s'."),
407 word.word(), from_ascii(encoding));
410 ::fputs(encoded.c_str(), out);
414 bool error = select(err_read);
417 error_ = _("Could not communicate with the ispell spellchecker process.");
422 // FIXME UNICODE: buf is not in UTF8, but probably the locale encoding
423 error_ = from_utf8(buf);
427 // I think we have to check if ispell is still alive here because
428 // the signal-handler could have disabled blocking on the fd
445 case '#': // Not found, no near misses and guesses
448 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
449 case '&': // Not found, but we have near misses
451 res = SUGGESTED_WORDS;
452 char * p = strpbrk(buf, ":");
453 str = new char[strlen(p) + 1];
458 default: // This shouldn't happen, but you know Murphy
463 if (res != IGNORED_WORD) {
464 /* wait for ispell to finish */
472 void ISpell::insert(WordLangTuple const & word)
474 string const encoded = to_iconv_encoding(word.word(), encoding);
475 if (encoded.empty()) {
477 _("Could not insert word `%1$s' because it could not be converted to encoding `%2$s'."),
478 word.word(), from_ascii(encoding));
481 ::fputc('*', out); // Insert word in personal dictionary
482 ::fputs(encoded.c_str(), out);
487 void ISpell::accept(WordLangTuple const & word)
489 string const encoded = to_iconv_encoding(word.word(), encoding);
490 if (encoded.empty()) {
492 _("Could not accept word `%1$s' because it could not be converted to encoding `%2$s'."),
493 word.word(), from_ascii(encoding));
496 ::fputc('@', out); // Accept in this session
497 ::fputs(encoded.c_str(), out);
502 docstring const ISpell::error()