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
47 class LaunchIspell : public lyx::support::ForkedProcess {
50 LaunchIspell(BufferParams const & p, string const & l,
51 int * in, int * out, int * err)
52 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
54 virtual auto_ptr<lyx::support::ForkedProcess> clone() const {
55 return auto_ptr<lyx::support::ForkedProcess>(new LaunchIspell(*this));
61 virtual int generateChild();
64 BufferParams const & params;
72 int LaunchIspell::start()
74 command_ = lyxrc.isp_command;
75 return runNonBlocking();
79 int LaunchIspell::generateChild()
81 pid_t isp_pid = fork();
84 // failed (-1) or parent process (>0)
89 dup2(pipein[0], STDIN_FILENO);
90 dup2(pipeout[1], STDOUT_FILENO);
91 dup2(pipeerr[1], STDERR_FILENO);
102 char * tmp = new char[lyxrc.isp_command.length() + 1];
103 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
104 tmp[lyxrc.isp_command.length()] = '\0';
107 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
110 if (lang != "default") {
112 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
114 tmp = new char[lang.length() + 1];
115 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
119 if (lyxrc.isp_accept_compound) {
120 // Consider run-together words as legal compounds
122 string("-C").copy(tmp, 2); tmp[2] = '\0';
125 // Report run-together words with
126 // missing blanks as errors
128 string("-B").copy(tmp, 2); tmp[2] = '\0';
131 if (lyxrc.isp_use_esc_chars) {
132 // Specify additional characters that
133 // can be part of a word
135 string("-w").copy(tmp, 2); tmp[2] = '\0';
137 // Put the escape chars in ""s
138 string tms = '"' + lyxrc.isp_esc_chars + '"';
139 tmp = new char[tms.length() + 1];
140 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
143 if (lyxrc.isp_use_pers_dict) {
144 // Specify an alternate personal dictionary
146 string("-p").copy(tmp, 2);
149 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
150 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
151 tmp[lyxrc.isp_pers_dict.length()] = '\0';
154 if (lyxrc.isp_use_input_encoding &&
155 params.inputenc != "default") {
156 string enc = (params.inputenc == "auto")
157 ? params.language->encoding()->LatexName()
159 string::size_type n = enc.length();
161 string("-T").copy(tmp, 2);
163 argv[argc++] = tmp; // Input encoding
164 tmp = new char[n + 1];
172 execvp(argv[0], const_cast<char * const *>(argv));
174 // free the memory used by string::copy in the
176 for (int i = 0; i < argc - 1; ++i)
179 lyxerr << "LyX: Failed to start ispell!" << endl;
187 ISpell::ISpell(BufferParams const & params, string const & lang)
188 : in(0), out(0), inerr(0), str(0)
190 lyxerr[Debug::GUI] << "Created ispell" << endl;
192 // static due to the setvbuf. Ugly.
193 static char o_buf[BUFSIZ];
195 // We need to throw an exception not do this
196 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
197 = pipeerr[0] = pipeerr[1] = -1;
199 // This is what happens when goto gets banned.
201 if (pipe(pipein) == -1) {
202 error_ = _("Can't create pipe for spellchecker.");
206 if (pipe(pipeout) == -1) {
209 error_ = _("Can't create pipe for spellchecker.");
213 if (pipe(pipeerr) == -1) {
218 error_ = _("Can't create pipe for spellchecker.");
222 if ((out = fdopen(pipein[1], "w")) == 0) {
223 error_ = _("Can't open pipe for spellchecker.");
227 if ((in = fdopen(pipeout[0], "r")) == 0) {
228 error_ = _("Can't open pipe for spellchecker.");
232 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
233 error_ = _("Can't open pipe for spellchecker.");
237 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
239 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
241 if (li->start() == -1) {
242 error_ = _("Could not create an ispell process.\nYou may not have "
243 "the right languages installed.");
248 /* Parent process: Read ispells identification message */
251 bool error = select(err_read);
255 // Set terse mode (silently accept correct words)
260 /* must have read something from stderr */
263 // select returned error
264 error_ = _("The spell process returned an error.\nPerhaps "
265 "it has been configured wrongly ?");
281 lyxerr[Debug::GUI] << "Killing ispell" << endl;
290 fputs("#\n", out); // Save personal dictionary
306 bool ISpell::select(bool & err_read)
312 FD_SET(pipeout[0], &infds);
313 FD_SET(pipeerr[0], &infds);
317 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
318 SELECT_TYPE_ARG234 (&infds),
321 SELECT_TYPE_ARG5 (&tv));
327 if (FD_ISSET(pipeerr[0], &infds)) {
328 fgets(buf, BUFSIZ, inerr);
333 fgets(buf, BUFSIZ, in);
339 string const ISpell::nextMiss()
341 // Well, somebody is a sick fuck.
343 if (str == 0 || *(e+1) == '\0')
346 e = strpbrk(b, ",\n");
356 return child_.get() && child_->running();
360 enum ISpell::Result ISpell::check(WordLangTuple const & word)
362 // FIXME Please rewrite to use string.
366 ::fputs(word.word().c_str(), out);
370 bool error = select(err_read);
373 error_ = _("Could not communicate with the spell-checker program.");
382 // I think we have to check if ispell is still alive here because
383 // the signal-handler could have disabled blocking on the fd
400 case '#': // Not found, no near misses and guesses
403 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
404 case '&': // Not found, but we have near misses
407 char * p = strpbrk(buf, ":");
408 str = new char[strlen(p) + 1];
413 default: // This shouldn't happen, but you know Murphy
419 /* wait for ispell to finish */
427 void ISpell::insert(WordLangTuple const & word)
429 ::fputc('*', out); // Insert word in personal dictionary
430 ::fputs(word.word().c_str(), out);
435 void ISpell::accept(WordLangTuple const & word)
437 ::fputc('@', out); // Accept in this session
438 ::fputs(word.word().c_str(), out);
443 string const ISpell::error()