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 #ifndef CXX_GLOBAL_CSTD
42 class LaunchIspell : public ForkedProcess {
45 LaunchIspell(BufferParams const & p, string const & l,
46 int * in, int * out, int * err)
47 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
49 virtual ForkedProcess * clone() const {
50 return new LaunchIspell(*this);
56 virtual int generateChild();
59 BufferParams const & params;
67 int LaunchIspell::start()
69 command_ = lyxrc.isp_command;
70 return runNonBlocking();
74 int LaunchIspell::generateChild()
76 pid_t isp_pid = fork();
79 // failed (-1) or parent process (>0)
84 dup2(pipein[0], STDIN_FILENO);
85 dup2(pipeout[1], STDOUT_FILENO);
86 dup2(pipeerr[1], STDERR_FILENO);
97 char * tmp = new char[lyxrc.isp_command.length() + 1];
98 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
99 tmp[lyxrc.isp_command.length()] = '\0';
102 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
105 if (lang != "default") {
107 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
109 tmp = new char[lang.length() + 1];
110 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
114 if (lyxrc.isp_accept_compound) {
115 // Consider run-together words as legal compounds
117 string("-C").copy(tmp, 2); tmp[2] = '\0';
120 // Report run-together words with
121 // missing blanks as errors
123 string("-B").copy(tmp, 2); tmp[2] = '\0';
126 if (lyxrc.isp_use_esc_chars) {
127 // Specify additional characters that
128 // can be part of a word
130 string("-w").copy(tmp, 2); tmp[2] = '\0';
132 // Put the escape chars in ""s
133 string tms = '"' + lyxrc.isp_esc_chars + '"';
134 tmp = new char[tms.length() + 1];
135 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
138 if (lyxrc.isp_use_pers_dict) {
139 // Specify an alternate personal dictionary
141 string("-p").copy(tmp, 2);
144 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
145 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
146 tmp[lyxrc.isp_pers_dict.length()] = '\0';
149 if (lyxrc.isp_use_input_encoding &&
150 params.inputenc != "default") {
151 string enc = (params.inputenc == "auto")
152 ? params.language->encoding()->LatexName()
154 string::size_type n = enc.length();
156 string("-T").copy(tmp, 2);
158 argv[argc++] = tmp; // Input encoding
159 tmp = new char[n + 1];
167 execvp(argv[0], const_cast<char * const *>(argv));
169 // free the memory used by string::copy in the
171 for (int i = 0; i < argc - 1; ++i)
174 lyxerr << "LyX: Failed to start ispell!" << endl;
182 ISpell::ISpell(BufferParams const & params, string const & lang)
183 : in(0), out(0), inerr(0), str(0)
185 lyxerr[Debug::GUI] << "Created ispell" << endl;
187 // static due to the setvbuf. Ugly.
188 static char o_buf[BUFSIZ];
190 // We need to throw an exception not do this
191 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
192 = pipeerr[0] = pipeerr[1] = -1;
194 // This is what happens when goto gets banned.
196 if (pipe(pipein) == -1) {
197 error_ = _("Can't create pipe for spellchecker.");
201 if (pipe(pipeout) == -1) {
204 error_ = _("Can't create pipe for spellchecker.");
208 if (pipe(pipeerr) == -1) {
213 error_ = _("Can't create pipe for spellchecker.");
217 if ((out = fdopen(pipein[1], "w")) == 0) {
218 error_ = _("Can't open pipe for spellchecker.");
222 if ((in = fdopen(pipeout[0], "r")) == 0) {
223 error_ = _("Can't open pipe for spellchecker.");
227 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
228 error_ = _("Can't open pipe for spellchecker.");
232 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
234 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
236 if (li->start() == -1) {
237 error_ = _("Could not create an ispell process.\nYou may not have "
238 " the right languages installed.");
243 /* Parent process: Read ispells identification message */
246 bool error = select(err_read);
250 // Set terse mode (silently accept correct words)
255 /* must have read something from stderr */
258 // select returned error
259 error_ = _("The spell process returned an error.\nPerhaps "
260 "it has been configured wrongly ?");
276 lyxerr[Debug::GUI] << "Killing ispell" << endl;
285 fputs("#\n", out); // Save personal dictionary
301 bool ISpell::select(bool & err_read)
307 FD_SET(pipeout[0], &infds);
308 FD_SET(pipeerr[0], &infds);
312 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
313 SELECT_TYPE_ARG234 (&infds),
316 SELECT_TYPE_ARG5 (&tv));
322 if (FD_ISSET(pipeerr[0], &infds)) {
323 fgets(buf, BUFSIZ, inerr);
328 fgets(buf, BUFSIZ, in);
334 string const ISpell::nextMiss()
336 // Well, somebody is a sick fuck.
338 if (str == 0 || *(e+1) == '\0')
341 e = strpbrk(b, ",\n");
351 return child_.get() && child_->running();
355 enum ISpell::Result ISpell::check(WordLangTuple const & word)
357 // FIXME Please rewrite to use string.
361 ::fputs(word.word().c_str(), out);
365 bool error = select(err_read);
368 error_ = _("Could not communicate with the spell-checker program");
377 // I think we have to check if ispell is still alive here because
378 // the signal-handler could have disabled blocking on the fd
395 case '#': // Not found, no near misses and guesses
398 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
399 case '&': // Not found, but we have near misses
402 char * p = strpbrk(buf, ":");
403 str = new char[strlen(p) + 1];
408 default: // This shouldn't happen, but you know Murphy
414 /* wait for ispell to finish */
422 void ISpell::insert(WordLangTuple const & word)
424 ::fputc('*', out); // Insert word in personal dictionary
425 ::fputs(word.word().c_str(), out);
430 void ISpell::accept(WordLangTuple const & word)
432 ::fputc('@', out); // Accept in this session
433 ::fputs(word.word().c_str(), out);
438 string const ISpell::error()