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.
21 #include "WordLangTuple.h"
23 #include "bufferparams.h"
25 #include "support/forkedcall.h"
26 #include "support/lstrings.h"
28 // HP-UX 11.x doesn't have this header
29 #ifdef HAVE_SYS_SELECT_H
30 #include <sys/select.h>
34 using namespace lyx::support;
36 #ifndef CXX_GLOBAL_CSTD
48 class LaunchIspell : public lyx::support::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 lyx::support::ForkedProcess * clone() const {
56 return new LaunchIspell(*this);
62 virtual int generateChild();
65 BufferParams const & params;
73 int LaunchIspell::start()
75 command_ = lyxrc.isp_command;
76 return runNonBlocking();
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 string::size_type 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;
188 ISpell::ISpell(BufferParams const & params, string const & lang)
189 : in(0), out(0), inerr(0), str(0)
191 lyxerr[Debug::GUI] << "Created ispell" << endl;
193 // static due to the setvbuf. Ugly.
194 static char o_buf[BUFSIZ];
196 // We need to throw an exception not do this
197 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
198 = pipeerr[0] = pipeerr[1] = -1;
200 // This is what happens when goto gets banned.
202 if (pipe(pipein) == -1) {
203 error_ = _("Can't create pipe for spellchecker.");
207 if (pipe(pipeout) == -1) {
210 error_ = _("Can't create pipe for spellchecker.");
214 if (pipe(pipeerr) == -1) {
219 error_ = _("Can't create pipe for spellchecker.");
223 if ((out = fdopen(pipein[1], "w")) == 0) {
224 error_ = _("Can't open pipe for spellchecker.");
228 if ((in = fdopen(pipeout[0], "r")) == 0) {
229 error_ = _("Can't open pipe for spellchecker.");
233 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
234 error_ = _("Can't open pipe for spellchecker.");
238 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
240 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
242 if (li->start() == -1) {
243 error_ = _("Could not create an ispell process.\nYou may not have "
244 "the right languages installed.");
249 /* Parent process: Read ispells identification message */
252 bool error = select(err_read);
256 // Set terse mode (silently accept correct words)
261 /* must have read something from stderr */
264 // select returned error
265 error_ = _("The spell process returned an error.\nPerhaps "
266 "it has been configured wrongly ?");
282 lyxerr[Debug::GUI] << "Killing ispell" << endl;
291 fputs("#\n", out); // Save personal dictionary
307 bool ISpell::select(bool & err_read)
313 FD_SET(pipeout[0], &infds);
314 FD_SET(pipeerr[0], &infds);
318 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
319 SELECT_TYPE_ARG234 (&infds),
322 SELECT_TYPE_ARG5 (&tv));
328 if (FD_ISSET(pipeerr[0], &infds)) {
329 fgets(buf, BUFSIZ, inerr);
334 fgets(buf, BUFSIZ, in);
340 string const ISpell::nextMiss()
342 // Well, somebody is a sick fuck.
344 if (str == 0 || *(e+1) == '\0')
347 e = strpbrk(b, ",\n");
357 return child_.get() && child_->running();
361 enum ISpell::Result ISpell::check(WordLangTuple const & word)
363 // FIXME Please rewrite to use string.
367 ::fputs(word.word().c_str(), out);
371 bool error = select(err_read);
374 error_ = _("Could not communicate with the spell-checker program.");
383 // I think we have to check if ispell is still alive here because
384 // the signal-handler could have disabled blocking on the fd
401 case '#': // Not found, no near misses and guesses
404 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
405 case '&': // Not found, but we have near misses
408 char * p = strpbrk(buf, ":");
409 str = new char[strlen(p) + 1];
414 default: // This shouldn't happen, but you know Murphy
420 /* wait for ispell to finish */
428 void ISpell::insert(WordLangTuple const & word)
430 ::fputc('*', out); // Insert word in personal dictionary
431 ::fputs(word.word().c_str(), out);
436 void ISpell::accept(WordLangTuple const & word)
438 ::fputc('@', out); // Accept in this session
439 ::fputs(word.word().c_str(), out);
444 string const ISpell::error()