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 #include <sys/select.h>
27 #ifndef CXX_GLOBAL_CSTD
39 class LaunchIspell : public ForkedProcess {
42 LaunchIspell(BufferParams const & p, string const & l,
43 int * in, int * out, int * err)
44 : params(p), lang(l), pipein(in), pipeout(out), pipeerr(err) {}
46 virtual ForkedProcess * clone() const {
47 return new LaunchIspell(*this);
53 virtual int generateChild();
56 BufferParams const & params;
64 int LaunchIspell::start()
66 command_ = lyxrc.isp_command;
67 return runNonBlocking();
71 int LaunchIspell::generateChild()
73 pid_t isp_pid = fork();
76 // failed (-1) or parent process (>0)
81 dup2(pipein[0], STDIN_FILENO);
82 dup2(pipeout[1], STDOUT_FILENO);
83 dup2(pipeerr[1], STDERR_FILENO);
94 char * tmp = new char[lyxrc.isp_command.length() + 1];
95 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
96 tmp[lyxrc.isp_command.length()] = '\0';
99 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
102 if (lang != "default") {
104 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
106 tmp = new char[lang.length() + 1];
107 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
111 if (lyxrc.isp_accept_compound) {
112 // Consider run-together words as legal compounds
114 string("-C").copy(tmp, 2); tmp[2] = '\0';
117 // Report run-together words with
118 // missing blanks as errors
120 string("-B").copy(tmp, 2); tmp[2] = '\0';
123 if (lyxrc.isp_use_esc_chars) {
124 // Specify additional characters that
125 // can be part of a word
127 string("-w").copy(tmp, 2); tmp[2] = '\0';
129 // Put the escape chars in ""s
130 string tms = '"' + lyxrc.isp_esc_chars + '"';
131 tmp = new char[tms.length() + 1];
132 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
135 if (lyxrc.isp_use_pers_dict) {
136 // Specify an alternate personal dictionary
138 string("-p").copy(tmp, 2);
141 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
142 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
143 tmp[lyxrc.isp_pers_dict.length()] = '\0';
146 if (lyxrc.isp_use_input_encoding &&
147 params.inputenc != "default") {
148 string enc = (params.inputenc == "auto")
149 ? params.language->encoding()->LatexName()
151 string::size_type n = enc.length();
153 string("-T").copy(tmp, 2);
155 argv[argc++] = tmp; // Input encoding
156 tmp = new char[n + 1];
164 execvp(argv[0], const_cast<char * const *>(argv));
166 // free the memory used by string::copy in the
168 for (int i = 0; i < argc - 1; ++i)
171 lyxerr << "LyX: Failed to start ispell!" << endl;
179 ISpell::ISpell(BufferParams const & params, string const & lang)
180 : in(0), out(0), inerr(0), str(0)
182 lyxerr[Debug::GUI] << "Created ispell" << endl;
184 // static due to the setvbuf. Ugly.
185 static char o_buf[BUFSIZ];
187 // We need to throw an exception not do this
188 pipein[0] = pipein[1] = pipeout[0] = pipeout[1]
189 = pipeerr[0] = pipeerr[1] = -1;
191 // This is what happens when goto gets banned.
193 if (pipe(pipein) == -1) {
194 error_ = _("Can't create pipe for spellchecker.");
198 if (pipe(pipeout) == -1) {
201 error_ = _("Can't create pipe for spellchecker.");
205 if (pipe(pipeerr) == -1) {
210 error_ = _("Can't create pipe for spellchecker.");
214 if ((out = fdopen(pipein[1], "w")) == 0) {
215 error_ = _("Can't open pipe for spellchecker.");
219 if ((in = fdopen(pipeout[0], "r")) == 0) {
220 error_ = _("Can't open pipe for spellchecker.");
224 if ((inerr = fdopen(pipeerr[0], "r")) == 0) {
225 error_ = _("Can't open pipe for spellchecker.");
229 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
231 LaunchIspell * li = new LaunchIspell(params, lang, pipein, pipeout, pipeerr);
233 if (li->start() == -1) {
234 error_ = _("Could not create an ispell process.\nYou may not have "
235 " the right languages installed.");
240 /* Parent process: Read ispells identification message */
243 bool error = select(err_read);
247 // Set terse mode (silently accept correct words)
252 /* must have read something from stderr */
255 // select returned error
256 error_ = _("The spell process returned an error.\nPerhaps "
257 "it has been configured wrongly ?");
273 lyxerr[Debug::GUI] << "Killing ispell" << endl;
282 fputs("#\n", out); // Save personal dictionary
298 bool ISpell::select(bool & err_read)
304 FD_SET(pipeout[0], &infds);
305 FD_SET(pipeerr[0], &infds);
309 retval = ::select(SELECT_TYPE_ARG1 (max(pipeout[0], pipeerr[0]) + 1),
310 SELECT_TYPE_ARG234 (&infds),
313 SELECT_TYPE_ARG5 (&tv));
319 if (FD_ISSET(pipeerr[0], &infds)) {
320 fgets(buf, BUFSIZ, inerr);
325 fgets(buf, BUFSIZ, in);
331 string const ISpell::nextMiss()
333 // Well, somebody is a sick fuck.
335 if (str == 0 || *(e+1) == '\0')
338 e = strpbrk(b, ",\n");
348 return child_.get() && child_->running();
352 enum ISpell::Result ISpell::check(WordLangTuple const & word)
354 // FIXME Please rewrite to use string.
358 ::fputs(word.word().c_str(), out);
362 bool error = select(err_read);
365 error_ = _("Could not communicate with the spell-checker program");
374 // I think we have to check if ispell is still alive here because
375 // the signal-handler could have disabled blocking on the fd
392 case '#': // Not found, no near misses and guesses
395 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
396 case '&': // Not found, but we have near misses
399 char * p = strpbrk(buf, ":");
400 str = new char[strlen(p) + 1];
405 default: // This shouldn't happen, but you know Murphy
411 /* wait for ispell to finish */
419 void ISpell::insert(WordLangTuple const & word)
421 ::fputc('*', out); // Insert word in personal dictionary
422 ::fputs(word.word().c_str(), out);
427 void ISpell::accept(WordLangTuple const & word)
429 ::fputc('@', out); // Accept in this session
430 ::fputs(word.word().c_str(), out);
435 string const ISpell::error()