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.
19 #include "WordLangTuple.h"
21 #include "bufferparams.h"
23 #include "support/forkedcall.h"
25 // HP-UX 11.x doesn't have this header
26 #ifdef HAVE_SYS_SELECT_H
29 using namespace lyx::support;
31 #ifndef CXX_GLOBAL_CSTD
43 class LaunchIspell : public lyx::support::ForkedProcess {
46 LaunchIspell(BufferParams const & p, string const & l,
47 int * in, int * out, int * err)
48 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
50 virtual lyx::support::ForkedProcess * clone() const {
51 return new LaunchIspell(*this);
57 virtual int generateChild();
60 BufferParams const & params;
68 int LaunchIspell::start()
70 command_ = lyxrc.isp_command;
71 return runNonBlocking();
75 int LaunchIspell::generateChild()
77 pid_t isp_pid = fork();
80 // failed (-1) or parent process (>0)
85 dup2(pipein[0], STDIN_FILENO);
86 dup2(pipeout[1], STDOUT_FILENO);
87 dup2(pipeerr[1], STDERR_FILENO);
98 char * tmp = new char[lyxrc.isp_command.length() + 1];
99 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
100 tmp[lyxrc.isp_command.length()] = '\0';
103 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
106 if (lang != "default") {
108 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
110 tmp = new char[lang.length() + 1];
111 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
115 if (lyxrc.isp_accept_compound) {
116 // Consider run-together words as legal compounds
118 string("-C").copy(tmp, 2); tmp[2] = '\0';
121 // Report run-together words with
122 // missing blanks as errors
124 string("-B").copy(tmp, 2); tmp[2] = '\0';
127 if (lyxrc.isp_use_esc_chars) {
128 // Specify additional characters that
129 // can be part of a word
131 string("-w").copy(tmp, 2); tmp[2] = '\0';
133 // Put the escape chars in ""s
134 string tms = '"' + lyxrc.isp_esc_chars + '"';
135 tmp = new char[tms.length() + 1];
136 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
139 if (lyxrc.isp_use_pers_dict) {
140 // Specify an alternate personal dictionary
142 string("-p").copy(tmp, 2);
145 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
146 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
147 tmp[lyxrc.isp_pers_dict.length()] = '\0';
150 if (lyxrc.isp_use_input_encoding &&
151 params.inputenc != "default") {
152 string enc = (params.inputenc == "auto")
153 ? params.language->encoding()->LatexName()
155 string::size_type n = enc.length();
157 string("-T").copy(tmp, 2);
159 argv[argc++] = tmp; // Input encoding
160 tmp = new char[n + 1];
168 execvp(argv[0], const_cast<char * const *>(argv));
170 // free the memory used by string::copy in the
172 for (int i = 0; i < argc - 1; ++i)
175 lyxerr << "LyX: Failed to start ispell!" << endl;
183 ISpell::ISpell(BufferParams const & params, string const & lang)
184 : in(0), out(0), inerr(0), str(0)
186 lyxerr[Debug::GUI] << "Created ispell" << endl;
188 // static due to the setvbuf. Ugly.
189 static char o_buf[BUFSIZ];
191 // We need to throw an exception not do this
192 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
193 = pipeerr[0] = pipeerr[1] = -1;
195 // This is what happens when goto gets banned.
197 if (pipe(pipein) == -1) {
198 error_ = _("Can't create pipe for spellchecker.");
202 if (pipe(pipeout) == -1) {
205 error_ = _("Can't create pipe for spellchecker.");
209 if (pipe(pipeerr) == -1) {
214 error_ = _("Can't create pipe for spellchecker.");
218 if ((out = fdopen(pipein[1], "w")) == 0) {
219 error_ = _("Can't open pipe for spellchecker.");
223 if ((in = fdopen(pipeout[0], "r")) == 0) {
224 error_ = _("Can't open pipe for spellchecker.");
228 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
229 error_ = _("Can't open pipe for spellchecker.");
233 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
235 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
237 if (li->start() == -1) {
238 error_ = _("Could not create an ispell process.\nYou may not have "
239 "the right languages installed.");
244 /* Parent process: Read ispells identification message */
247 bool error = select(err_read);
251 // Set terse mode (silently accept correct words)
256 /* must have read something from stderr */
259 // select returned error
260 error_ = _("The spell process returned an error.\nPerhaps "
261 "it has been configured wrongly ?");
277 lyxerr[Debug::GUI] << "Killing ispell" << endl;
286 fputs("#\n", out); // Save personal dictionary
302 bool ISpell::select(bool & err_read)
308 FD_SET(pipeout[0], &infds);
309 FD_SET(pipeerr[0], &infds);
313 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
314 SELECT_TYPE_ARG234 (&infds),
317 SELECT_TYPE_ARG5 (&tv));
323 if (FD_ISSET(pipeerr[0], &infds)) {
324 fgets(buf, BUFSIZ, inerr);
329 fgets(buf, BUFSIZ, in);
335 string const ISpell::nextMiss()
337 // Well, somebody is a sick fuck.
339 if (str == 0 || *(e+1) == '\0')
342 e = strpbrk(b, ",\n");
352 return child_.get() && child_->running();
356 enum ISpell::Result ISpell::check(WordLangTuple const & word)
358 // FIXME Please rewrite to use string.
362 ::fputs(word.word().c_str(), out);
366 bool error = select(err_read);
369 error_ = _("Could not communicate with the spell-checker program.");
378 // I think we have to check if ispell is still alive here because
379 // the signal-handler could have disabled blocking on the fd
396 case '#': // Not found, no near misses and guesses
399 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
400 case '&': // Not found, but we have near misses
403 char * p = strpbrk(buf, ":");
404 str = new char[strlen(p) + 1];
409 default: // This shouldn't happen, but you know Murphy
415 /* wait for ispell to finish */
423 void ISpell::insert(WordLangTuple const & word)
425 ::fputc('*', out); // Insert word in personal dictionary
426 ::fputs(word.word().c_str(), out);
431 void ISpell::accept(WordLangTuple const & word)
433 ::fputc('@', out); // Accept in this session
434 ::fputs(word.word().c_str(), out);
439 string const ISpell::error()