3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <levon@movementarian.org>
18 #include "WordLangTuple.h"
21 #include "support/forkedcall.h"
22 #include "support/lstrings.h"
24 // HP-UX 11.x doesn't have this header
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
30 using namespace lyx::support;
32 #ifndef CXX_GLOBAL_CSTD
44 class LaunchIspell : public lyx::support::ForkedProcess {
47 LaunchIspell(BufferParams const & p, string const & l,
48 int * in, int * out, int * err)
49 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
51 virtual lyx::support::ForkedProcess * clone() const {
52 return new LaunchIspell(*this);
58 virtual int generateChild();
61 BufferParams const & params;
69 int LaunchIspell::start()
71 command_ = lyxrc.isp_command;
72 return runNonBlocking();
76 int LaunchIspell::generateChild()
78 pid_t isp_pid = fork();
81 // failed (-1) or parent process (>0)
86 dup2(pipein[0], STDIN_FILENO);
87 dup2(pipeout[1], STDOUT_FILENO);
88 dup2(pipeerr[1], STDERR_FILENO);
99 char * tmp = new char[lyxrc.isp_command.length() + 1];
100 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
101 tmp[lyxrc.isp_command.length()] = '\0';
104 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
107 if (lang != "default") {
109 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
111 tmp = new char[lang.length() + 1];
112 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
116 if (lyxrc.isp_accept_compound) {
117 // Consider run-together words as legal compounds
119 string("-C").copy(tmp, 2); tmp[2] = '\0';
122 // Report run-together words with
123 // missing blanks as errors
125 string("-B").copy(tmp, 2); tmp[2] = '\0';
128 if (lyxrc.isp_use_esc_chars) {
129 // Specify additional characters that
130 // can be part of a word
132 string("-w").copy(tmp, 2); tmp[2] = '\0';
134 // Put the escape chars in ""s
135 string tms = '"' + lyxrc.isp_esc_chars + '"';
136 tmp = new char[tms.length() + 1];
137 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
140 if (lyxrc.isp_use_pers_dict) {
141 // Specify an alternate personal dictionary
143 string("-p").copy(tmp, 2);
146 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
147 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
148 tmp[lyxrc.isp_pers_dict.length()] = '\0';
151 if (lyxrc.isp_use_input_encoding &&
152 params.inputenc != "default") {
153 string enc = (params.inputenc == "auto")
154 ? params.language->encoding()->LatexName()
156 string::size_type n = enc.length();
158 string("-T").copy(tmp, 2);
160 argv[argc++] = tmp; // Input encoding
161 tmp = new char[n + 1];
169 execvp(argv[0], const_cast<char * const *>(argv));
171 // free the memory used by string::copy in the
173 for (int i = 0; i < argc - 1; ++i)
176 lyxerr << "LyX: Failed to start ispell!" << endl;
184 ISpell::ISpell(BufferParams const & params, string const & lang)
185 : in(0), out(0), inerr(0), str(0)
187 lyxerr[Debug::GUI] << "Created ispell" << endl;
189 // static due to the setvbuf. Ugly.
190 static char o_buf[BUFSIZ];
192 // We need to throw an exception not do this
193 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
194 = pipeerr[0] = pipeerr[1] = -1;
196 // This is what happens when goto gets banned.
198 if (pipe(pipein) == -1) {
199 error_ = _("Can't create pipe for spellchecker.");
203 if (pipe(pipeout) == -1) {
206 error_ = _("Can't create pipe for spellchecker.");
210 if (pipe(pipeerr) == -1) {
215 error_ = _("Can't create pipe for spellchecker.");
219 if ((out = fdopen(pipein[1], "w")) == 0) {
220 error_ = _("Can't open pipe for spellchecker.");
224 if ((in = fdopen(pipeout[0], "r")) == 0) {
225 error_ = _("Can't open pipe for spellchecker.");
229 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
230 error_ = _("Can't open pipe for spellchecker.");
234 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
236 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
238 if (li->start() == -1) {
239 error_ = _("Could not create an ispell process.\nYou may not have "
240 "the right languages installed.");
245 /* Parent process: Read ispells identification message */
248 bool error = select(err_read);
252 // Set terse mode (silently accept correct words)
257 /* must have read something from stderr */
260 // select returned error
261 error_ = _("The spell process returned an error.\nPerhaps "
262 "it has been configured wrongly ?");
278 lyxerr[Debug::GUI] << "Killing ispell" << endl;
287 fputs("#\n", out); // Save personal dictionary
303 bool ISpell::select(bool & err_read)
309 FD_SET(pipeout[0], &infds);
310 FD_SET(pipeerr[0], &infds);
314 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
315 SELECT_TYPE_ARG234 (&infds),
318 SELECT_TYPE_ARG5 (&tv));
324 if (FD_ISSET(pipeerr[0], &infds)) {
325 fgets(buf, BUFSIZ, inerr);
330 fgets(buf, BUFSIZ, in);
336 string const ISpell::nextMiss()
338 // Well, somebody is a sick fuck.
340 if (str == 0 || *(e+1) == '\0')
343 e = strpbrk(b, ",\n");
353 return child_.get() && child_->running();
357 enum ISpell::Result ISpell::check(WordLangTuple const & word)
359 // FIXME Please rewrite to use string.
363 ::fputs(word.word().c_str(), out);
367 bool error = select(err_read);
370 error_ = _("Could not communicate with the spell-checker program.");
379 // I think we have to check if ispell is still alive here because
380 // the signal-handler could have disabled blocking on the fd
397 case '#': // Not found, no near misses and guesses
400 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
401 case '&': // Not found, but we have near misses
404 char * p = strpbrk(buf, ":");
405 str = new char[strlen(p) + 1];
410 default: // This shouldn't happen, but you know Murphy
416 /* wait for ispell to finish */
424 void ISpell::insert(WordLangTuple const & word)
426 ::fputc('*', out); // Insert word in personal dictionary
427 ::fputs(word.word().c_str(), out);
432 void ISpell::accept(WordLangTuple const & word)
434 ::fputc('@', out); // Accept in this session
435 ::fputs(word.word().c_str(), out);
440 string const ISpell::error()