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"
27 // HP-UX 11.x doesn't have this header
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
33 #ifndef CXX_GLOBAL_CSTD
46 class LaunchIspell : public lyx::support::ForkedProcess {
49 LaunchIspell(BufferParams const & p, string const & l,
50 int * in, int * out, int * err)
51 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
53 virtual lyx::support::ForkedProcess * clone() const {
54 return new LaunchIspell(*this);
60 virtual int generateChild();
63 BufferParams const & params;
71 int LaunchIspell::start()
73 command_ = lyxrc.isp_command;
74 return runNonBlocking();
78 int LaunchIspell::generateChild()
80 pid_t isp_pid = fork();
83 // failed (-1) or parent process (>0)
88 dup2(pipein[0], STDIN_FILENO);
89 dup2(pipeout[1], STDOUT_FILENO);
90 dup2(pipeerr[1], STDERR_FILENO);
101 char * tmp = new char[lyxrc.isp_command.length() + 1];
102 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
103 tmp[lyxrc.isp_command.length()] = '\0';
106 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
109 if (lang != "default") {
111 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
113 tmp = new char[lang.length() + 1];
114 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
118 if (lyxrc.isp_accept_compound) {
119 // Consider run-together words as legal compounds
121 string("-C").copy(tmp, 2); tmp[2] = '\0';
124 // Report run-together words with
125 // missing blanks as errors
127 string("-B").copy(tmp, 2); tmp[2] = '\0';
130 if (lyxrc.isp_use_esc_chars) {
131 // Specify additional characters that
132 // can be part of a word
134 string("-w").copy(tmp, 2); tmp[2] = '\0';
136 // Put the escape chars in ""s
137 string tms = '"' + lyxrc.isp_esc_chars + '"';
138 tmp = new char[tms.length() + 1];
139 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
142 if (lyxrc.isp_use_pers_dict) {
143 // Specify an alternate personal dictionary
145 string("-p").copy(tmp, 2);
148 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
149 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
150 tmp[lyxrc.isp_pers_dict.length()] = '\0';
153 if (lyxrc.isp_use_input_encoding &&
154 params.inputenc != "default") {
155 string enc = (params.inputenc == "auto")
156 ? params.language->encoding()->LatexName()
158 string::size_type n = enc.length();
160 string("-T").copy(tmp, 2);
162 argv[argc++] = tmp; // Input encoding
163 tmp = new char[n + 1];
171 execvp(argv[0], const_cast<char * const *>(argv));
173 // free the memory used by string::copy in the
175 for (int i = 0; i < argc - 1; ++i)
178 lyxerr << "LyX: Failed to start ispell!" << endl;
186 ISpell::ISpell(BufferParams const & params, string const & lang)
187 : in(0), out(0), inerr(0), str(0)
189 lyxerr[Debug::GUI] << "Created ispell" << endl;
191 // static due to the setvbuf. Ugly.
192 static char o_buf[BUFSIZ];
194 // We need to throw an exception not do this
195 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
196 = pipeerr[0] = pipeerr[1] = -1;
198 // This is what happens when goto gets banned.
200 if (pipe(pipein) == -1) {
201 error_ = _("Can't create pipe for spellchecker.");
205 if (pipe(pipeout) == -1) {
208 error_ = _("Can't create pipe for spellchecker.");
212 if (pipe(pipeerr) == -1) {
217 error_ = _("Can't create pipe for spellchecker.");
221 if ((out = fdopen(pipein[1], "w")) == 0) {
222 error_ = _("Can't open pipe for spellchecker.");
226 if ((in = fdopen(pipeout[0], "r")) == 0) {
227 error_ = _("Can't open pipe for spellchecker.");
231 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
232 error_ = _("Can't open pipe for spellchecker.");
236 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
238 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
240 if (li->start() == -1) {
241 error_ = _("Could not create an ispell process.\nYou may not have "
242 "the right languages installed.");
247 /* Parent process: Read ispells identification message */
250 bool error = select(err_read);
254 // Set terse mode (silently accept correct words)
259 /* must have read something from stderr */
262 // select returned error
263 error_ = _("The spell process returned an error.\nPerhaps "
264 "it has been configured wrongly ?");
280 lyxerr[Debug::GUI] << "Killing ispell" << endl;
289 fputs("#\n", out); // Save personal dictionary
305 bool ISpell::select(bool & err_read)
311 FD_SET(pipeout[0], &infds);
312 FD_SET(pipeerr[0], &infds);
316 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
317 SELECT_TYPE_ARG234 (&infds),
320 SELECT_TYPE_ARG5 (&tv));
326 if (FD_ISSET(pipeerr[0], &infds)) {
327 fgets(buf, BUFSIZ, inerr);
332 fgets(buf, BUFSIZ, in);
338 string const ISpell::nextMiss()
340 // Well, somebody is a sick fuck.
342 if (str == 0 || *(e+1) == '\0')
345 e = strpbrk(b, ",\n");
355 return child_.get() && child_->running();
359 enum ISpell::Result ISpell::check(WordLangTuple const & word)
361 // FIXME Please rewrite to use string.
365 ::fputs(word.word().c_str(), out);
369 bool error = select(err_read);
372 error_ = _("Could not communicate with the spell-checker program.");
381 // I think we have to check if ispell is still alive here because
382 // the signal-handler could have disabled blocking on the fd
399 case '#': // Not found, no near misses and guesses
402 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
403 case '&': // Not found, but we have near misses
406 char * p = strpbrk(buf, ":");
407 str = new char[strlen(p) + 1];
412 default: // This shouldn't happen, but you know Murphy
418 /* wait for ispell to finish */
426 void ISpell::insert(WordLangTuple const & word)
428 ::fputc('*', out); // Insert word in personal dictionary
429 ::fputs(word.word().c_str(), out);
434 void ISpell::accept(WordLangTuple const & word)
436 ::fputc('@', out); // Accept in this session
437 ::fputs(word.word().c_str(), out);
442 string const ISpell::error()