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 using boost::shared_ptr;
35 #ifndef CXX_GLOBAL_CSTD
48 class LaunchIspell : public lyx::support::ForkedProcess {
49 typedef lyx::support::ForkedProcess ForkedProcess;
52 LaunchIspell(BufferParams const & p, string const & l,
53 int * in, int * out, int * err)
54 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
56 virtual shared_ptr<ForkedProcess> clone() const {
57 return shared_ptr<ForkedProcess>(new LaunchIspell(*this));
63 virtual int generateChild();
66 BufferParams const & params;
74 int LaunchIspell::start()
76 command_ = lyxrc.isp_command;
81 int LaunchIspell::generateChild()
83 pid_t isp_pid = fork();
86 // failed (-1) or parent process (>0)
91 dup2(pipein[0], STDIN_FILENO);
92 dup2(pipeout[1], STDOUT_FILENO);
93 dup2(pipeerr[1], STDERR_FILENO);
104 char * tmp = new char[lyxrc.isp_command.length() + 1];
105 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
106 tmp[lyxrc.isp_command.length()] = '\0';
109 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
112 if (lang != "default") {
114 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
116 tmp = new char[lang.length() + 1];
117 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
121 if (lyxrc.isp_accept_compound) {
122 // Consider run-together words as legal compounds
124 string("-C").copy(tmp, 2); tmp[2] = '\0';
127 // Report run-together words with
128 // missing blanks as errors
130 string("-B").copy(tmp, 2); tmp[2] = '\0';
133 if (lyxrc.isp_use_esc_chars) {
134 // Specify additional characters that
135 // can be part of a word
137 string("-w").copy(tmp, 2); tmp[2] = '\0';
139 // Put the escape chars in ""s
140 string tms = '"' + lyxrc.isp_esc_chars + '"';
141 tmp = new char[tms.length() + 1];
142 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
145 if (lyxrc.isp_use_pers_dict) {
146 // Specify an alternate personal dictionary
148 string("-p").copy(tmp, 2);
151 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
152 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
153 tmp[lyxrc.isp_pers_dict.length()] = '\0';
156 if (lyxrc.isp_use_input_encoding &&
157 params.inputenc != "default") {
158 string enc = (params.inputenc == "auto")
159 ? params.language->encoding()->LatexName()
161 string::size_type n = enc.length();
163 string("-T").copy(tmp, 2);
165 argv[argc++] = tmp; // Input encoding
166 tmp = new char[n + 1];
174 execvp(argv[0], const_cast<char * const *>(argv));
176 // free the memory used by string::copy in the
178 for (int i = 0; i < argc - 1; ++i)
181 lyxerr << "LyX: Failed to start ispell!" << endl;
189 ISpell::ISpell(BufferParams const & params, string const & lang)
190 : in(0), out(0), inerr(0), str(0)
192 lyxerr[Debug::GUI] << "Created ispell" << endl;
194 // static due to the setvbuf. Ugly.
195 static char o_buf[BUFSIZ];
197 // We need to throw an exception not do this
198 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
199 = pipeerr[0] = pipeerr[1] = -1;
201 // This is what happens when goto gets banned.
203 if (pipe(pipein) == -1) {
204 error_ = _("Can't create pipe for spellchecker.");
208 if (pipe(pipeout) == -1) {
211 error_ = _("Can't create pipe for spellchecker.");
215 if (pipe(pipeerr) == -1) {
220 error_ = _("Can't create pipe for spellchecker.");
224 if ((out = fdopen(pipein[1], "w")) == 0) {
225 error_ = _("Can't open pipe for spellchecker.");
229 if ((in = fdopen(pipeout[0], "r")) == 0) {
230 error_ = _("Can't open pipe for spellchecker.");
234 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
235 error_ = _("Can't open pipe for spellchecker.");
239 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
241 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
243 if (li->start() == -1) {
244 error_ = _("Could not create an ispell process.\nYou may not have "
245 "the right languages installed.");
250 /* Parent process: Read ispells identification message */
253 bool error = select(err_read);
257 // Set terse mode (silently accept correct words)
262 /* must have read something from stderr */
265 // select returned error
266 error_ = _("The spell process returned an error.\nPerhaps "
267 "it has been configured wrongly ?");
283 lyxerr[Debug::GUI] << "Killing ispell" << endl;
292 fputs("#\n", out); // Save personal dictionary
308 bool ISpell::select(bool & err_read)
314 FD_SET(pipeout[0], &infds);
315 FD_SET(pipeerr[0], &infds);
319 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
320 SELECT_TYPE_ARG234 (&infds),
323 SELECT_TYPE_ARG5 (&tv));
329 if (FD_ISSET(pipeerr[0], &infds)) {
330 fgets(buf, BUFSIZ, inerr);
335 fgets(buf, BUFSIZ, in);
341 string const ISpell::nextMiss()
343 // Well, somebody is a sick fuck.
345 if (str == 0 || *(e+1) == '\0')
348 e = strpbrk(b, ",\n");
358 return child_.get() && child_->running();
362 enum ISpell::Result ISpell::check(WordLangTuple const & word)
364 // FIXME Please rewrite to use string.
368 ::fputs(word.word().c_str(), out);
372 bool error = select(err_read);
375 error_ = _("Could not communicate with the spell-checker program.");
384 // I think we have to check if ispell is still alive here because
385 // the signal-handler could have disabled blocking on the fd
402 case '#': // Not found, no near misses and guesses
405 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
406 case '&': // Not found, but we have near misses
409 char * p = strpbrk(buf, ":");
410 str = new char[strlen(p) + 1];
415 default: // This shouldn't happen, but you know Murphy
421 /* wait for ispell to finish */
429 void ISpell::insert(WordLangTuple const & word)
431 ::fputc('*', out); // Insert word in personal dictionary
432 ::fputs(word.word().c_str(), out);
437 void ISpell::accept(WordLangTuple const & word)
439 ::fputc('@', out); // Accept in this session
440 ::fputs(word.word().c_str(), out);
445 string const ISpell::error()