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>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
35 using boost::shared_ptr;
37 #ifndef CXX_GLOBAL_CSTD
50 class LaunchIspell : public lyx::support::ForkedProcess {
51 typedef lyx::support::ForkedProcess ForkedProcess;
54 LaunchIspell(BufferParams const & p, string const & l,
55 int * in, int * out, int * err)
56 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
58 virtual shared_ptr<ForkedProcess> clone() const {
59 return shared_ptr<ForkedProcess>(new LaunchIspell(*this));
65 virtual int generateChild();
68 BufferParams const & params;
76 int LaunchIspell::start()
78 command_ = lyxrc.isp_command;
83 int LaunchIspell::generateChild()
85 pid_t isp_pid = fork();
88 // failed (-1) or parent process (>0)
93 dup2(pipein[0], STDIN_FILENO);
94 dup2(pipeout[1], STDOUT_FILENO);
95 dup2(pipeerr[1], STDERR_FILENO);
106 char * tmp = new char[lyxrc.isp_command.length() + 1];
107 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
108 tmp[lyxrc.isp_command.length()] = '\0';
111 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
114 if (lang != "default") {
116 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
118 tmp = new char[lang.length() + 1];
119 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
123 if (lyxrc.isp_accept_compound) {
124 // Consider run-together words as legal compounds
126 string("-C").copy(tmp, 2); tmp[2] = '\0';
129 // Report run-together words with
130 // missing blanks as errors
132 string("-B").copy(tmp, 2); tmp[2] = '\0';
135 if (lyxrc.isp_use_esc_chars) {
136 // Specify additional characters that
137 // can be part of a word
139 string("-w").copy(tmp, 2); tmp[2] = '\0';
141 // Put the escape chars in ""s
142 string tms = '"' + lyxrc.isp_esc_chars + '"';
143 tmp = new char[tms.length() + 1];
144 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
147 if (lyxrc.isp_use_pers_dict) {
148 // Specify an alternate personal dictionary
150 string("-p").copy(tmp, 2);
153 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
154 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
155 tmp[lyxrc.isp_pers_dict.length()] = '\0';
158 if (lyxrc.isp_use_input_encoding &&
159 params.inputenc != "default") {
160 string enc = (params.inputenc == "auto")
161 ? params.language->encoding()->LatexName()
163 string::size_type n = enc.length();
165 string("-T").copy(tmp, 2);
167 argv[argc++] = tmp; // Input encoding
168 tmp = new char[n + 1];
176 execvp(argv[0], const_cast<char * const *>(argv));
178 // free the memory used by string::copy in the
180 for (int i = 0; i < argc - 1; ++i)
183 lyxerr << "LyX: Failed to start ispell!" << endl;
191 ISpell::ISpell(BufferParams const & params, string const & lang)
192 : in(0), out(0), inerr(0), str(0)
194 lyxerr[Debug::GUI] << "Created ispell" << endl;
196 // static due to the setvbuf. Ugly.
197 static char o_buf[BUFSIZ];
199 // We need to throw an exception not do this
200 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
201 = pipeerr[0] = pipeerr[1] = -1;
203 // This is what happens when goto gets banned.
205 if (pipe(pipein) == -1) {
206 error_ = _("Can't create pipe for spellchecker.");
210 if (pipe(pipeout) == -1) {
213 error_ = _("Can't create pipe for spellchecker.");
217 if (pipe(pipeerr) == -1) {
222 error_ = _("Can't create pipe for spellchecker.");
226 if ((out = fdopen(pipein[1], "w")) == 0) {
227 error_ = _("Can't open pipe for spellchecker.");
231 if ((in = fdopen(pipeout[0], "r")) == 0) {
232 error_ = _("Can't open pipe for spellchecker.");
236 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
237 error_ = _("Can't open pipe for spellchecker.");
241 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
243 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
245 if (li->start() == -1) {
246 error_ = _("Could not create an ispell process.\nYou may not have "
247 "the right languages installed.");
252 /* Parent process: Read ispells identification message */
255 bool error = select(err_read);
259 // Set terse mode (silently accept correct words)
264 /* must have read something from stderr */
267 // select returned error
268 error_ = _("The spell process returned an error.\nPerhaps "
269 "it has been configured wrongly ?");
285 lyxerr[Debug::GUI] << "Killing ispell" << endl;
294 fputs("#\n", out); // Save personal dictionary
310 bool ISpell::select(bool & err_read)
316 FD_SET(pipeout[0], &infds);
317 FD_SET(pipeerr[0], &infds);
321 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
322 SELECT_TYPE_ARG234 (&infds),
325 SELECT_TYPE_ARG5 (&tv));
331 if (FD_ISSET(pipeerr[0], &infds)) {
332 fgets(buf, BUFSIZ, inerr);
337 fgets(buf, BUFSIZ, in);
343 string const ISpell::nextMiss()
345 // Well, somebody is a sick fuck.
347 if (str == 0 || *(e+1) == '\0')
350 e = strpbrk(b, ",\n");
360 return child_.get() && child_->running();
364 enum ISpell::Result ISpell::check(WordLangTuple const & word)
366 // FIXME Please rewrite to use string.
370 ::fputs(word.word().c_str(), out);
374 bool error = select(err_read);
377 error_ = _("Could not communicate with the spell-checker program.");
386 // I think we have to check if ispell is still alive here because
387 // the signal-handler could have disabled blocking on the fd
404 case '#': // Not found, no near misses and guesses
407 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
408 case '&': // Not found, but we have near misses
410 res = SUGGESTED_WORDS;
411 char * p = strpbrk(buf, ":");
412 str = new char[strlen(p) + 1];
417 default: // This shouldn't happen, but you know Murphy
422 if (res != IGNORED_WORD) {
423 /* wait for ispell to finish */
431 void ISpell::insert(WordLangTuple const & word)
433 ::fputc('*', out); // Insert word in personal dictionary
434 ::fputs(word.word().c_str(), out);
439 void ISpell::accept(WordLangTuple const & word)
441 ::fputc('@', out); // Accept in this session
442 ::fputs(word.word().c_str(), out);
447 string const ISpell::error()