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>
37 using boost::shared_ptr;
39 #ifndef CXX_GLOBAL_CSTD
52 class LaunchIspell : public lyx::support::ForkedProcess {
53 typedef lyx::support::ForkedProcess ForkedProcess;
56 LaunchIspell(BufferParams const & p, string const & l,
57 int * in, int * out, int * err)
58 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
60 virtual shared_ptr<ForkedProcess> clone() const {
61 return shared_ptr<ForkedProcess>(new LaunchIspell(*this));
67 virtual int generateChild();
70 BufferParams const & params;
78 int LaunchIspell::start()
80 command_ = lyxrc.isp_command;
85 int LaunchIspell::generateChild()
87 pid_t isp_pid = fork();
90 // failed (-1) or parent process (>0)
95 dup2(pipein[0], STDIN_FILENO);
96 dup2(pipeout[1], STDOUT_FILENO);
97 dup2(pipeerr[1], STDERR_FILENO);
108 char * tmp = new char[lyxrc.isp_command.length() + 1];
109 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
110 tmp[lyxrc.isp_command.length()] = '\0';
113 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
116 if (lang != "default") {
118 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
120 tmp = new char[lang.length() + 1];
121 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
125 if (lyxrc.isp_accept_compound) {
126 // Consider run-together words as legal compounds
128 string("-C").copy(tmp, 2); tmp[2] = '\0';
131 // Report run-together words with
132 // missing blanks as errors
134 string("-B").copy(tmp, 2); tmp[2] = '\0';
137 if (lyxrc.isp_use_esc_chars) {
138 // Specify additional characters that
139 // can be part of a word
141 string("-w").copy(tmp, 2); tmp[2] = '\0';
143 // Put the escape chars in ""s
144 string tms = '"' + lyxrc.isp_esc_chars + '"';
145 tmp = new char[tms.length() + 1];
146 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
149 if (lyxrc.isp_use_pers_dict) {
150 // Specify an alternate personal dictionary
152 string("-p").copy(tmp, 2);
155 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
156 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
157 tmp[lyxrc.isp_pers_dict.length()] = '\0';
160 if (lyxrc.isp_use_input_encoding &&
161 params.inputenc != "default") {
162 string enc = (params.inputenc == "auto")
163 ? params.language->encoding()->latexName()
165 string::size_type n = enc.length();
167 string("-T").copy(tmp, 2);
169 argv[argc++] = tmp; // Input encoding
170 tmp = new char[n + 1];
178 execvp(argv[0], const_cast<char * const *>(argv));
180 // free the memory used by string::copy in the
182 for (int i = 0; i < argc - 1; ++i)
185 lyxerr << "LyX: Failed to start ispell!" << endl;
193 ISpell::ISpell(BufferParams const & params, string const & lang)
194 : in(0), out(0), inerr(0), str(0)
196 lyxerr[Debug::GUI] << "Created ispell" << endl;
198 // static due to the setvbuf. Ugly.
199 static char o_buf[BUFSIZ];
201 // We need to throw an exception not do this
202 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
203 = pipeerr[0] = pipeerr[1] = -1;
205 // This is what happens when goto gets banned.
207 if (pipe(pipein) == -1) {
208 error_ = _("Can't create pipe for spellchecker.");
212 if (pipe(pipeout) == -1) {
215 error_ = _("Can't create pipe for spellchecker.");
219 if (pipe(pipeerr) == -1) {
224 error_ = _("Can't create pipe for spellchecker.");
228 if ((out = fdopen(pipein[1], "w")) == 0) {
229 error_ = _("Can't open pipe for spellchecker.");
233 if ((in = fdopen(pipeout[0], "r")) == 0) {
234 error_ = _("Can't open pipe for spellchecker.");
238 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
239 error_ = _("Can't open pipe for spellchecker.");
243 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
245 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
247 if (li->start() == -1) {
248 error_ = _("Could not create an ispell process.\nYou may not have "
249 "the right languages installed.");
254 /* Parent process: Read ispells identification message */
257 bool error = select(err_read);
261 // Set terse mode (silently accept correct words)
266 /* must have read something from stderr */
267 error_ =lyx::from_utf8(buf);
269 // select returned error
270 error_ = _("The ispell process returned an error.\nPerhaps "
271 "it has been configured wrongly ?");
287 lyxerr[Debug::GUI] << "Killing ispell" << endl;
296 fputs("#\n", out); // Save personal dictionary
312 bool ISpell::select(bool & err_read)
318 FD_SET(pipeout[0], &infds);
319 FD_SET(pipeerr[0], &infds);
323 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
324 SELECT_TYPE_ARG234 (&infds),
327 SELECT_TYPE_ARG5 (&tv));
333 if (FD_ISSET(pipeerr[0], &infds)) {
334 fgets(buf, BUFSIZ, inerr);
339 fgets(buf, BUFSIZ, in);
345 string const ISpell::nextMiss()
347 // Well, somebody is a sick fuck.
349 if (str == 0 || *(e+1) == '\0')
352 e = strpbrk(b, ",\n");
362 return child_.get() && child_->running();
366 enum ISpell::Result ISpell::check(WordLangTuple const & word)
368 // FIXME Please rewrite to use string.
372 ::fputs(word.word().c_str(), out);
376 bool error = select(err_read);
379 error_ = _("Could not communicate with the ispell spellchecker process.");
384 error_ = lyx::from_utf8(buf);
388 // I think we have to check if ispell is still alive here because
389 // the signal-handler could have disabled blocking on the fd
406 case '#': // Not found, no near misses and guesses
409 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
410 case '&': // Not found, but we have near misses
412 res = SUGGESTED_WORDS;
413 char * p = strpbrk(buf, ":");
414 str = new char[strlen(p) + 1];
419 default: // This shouldn't happen, but you know Murphy
424 if (res != IGNORED_WORD) {
425 /* wait for ispell to finish */
433 void ISpell::insert(WordLangTuple const & word)
435 ::fputc('*', out); // Insert word in personal dictionary
436 ::fputs(word.word().c_str(), out);
441 void ISpell::accept(WordLangTuple const & word)
443 ::fputc('@', out); // Accept in this session
444 ::fputs(word.word().c_str(), out);
449 docstring const ISpell::error()