1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
11 * \author Kevin Atkinson
17 #pragma implementation
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
35 #ifdef HAVE_SYS_SELECT_H
36 # ifdef HAVE_STRINGS_H
37 // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
38 // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
43 #include <sys/select.h>
47 #include "support/lstrings.h"
51 #include "sp_ispell.h"
56 /// pid for the `ispell' process.
60 /// can be found in src/insets/figinset.C
61 extern void sigchldchecker(pid_t pid, int * status);
64 // ------------------- start special pspell code/class --------------------
68 #include "support/LAssert.h"
70 #define USE_ORIGINAL_MANAGER_FUNCS 1
71 # include <pspell/pspell.h>
73 #include "sp_pspell.h"
77 : sc(0), els(0), spell_error_object(0), flag(ISP_UNKNOWN)
82 PSpell::PSpell(BufferParams const & params, string const & lang)
83 : sc(0), els(0), spell_error_object(0), flag(ISP_UNKNOWN)
85 initialize(params, lang);
94 delete_pspell_string_emulation(els);
98 void PSpell::initialize(BufferParams const &, string const & lang)
100 PspellConfig * config = new_pspell_config();
101 config->replace("language-tag", lang.c_str());
102 spell_error_object = new_pspell_manager(config);
103 if (pspell_error_number(spell_error_object) != 0) {
104 error_ = pspell_error_message(spell_error_object);
107 sc = to_pspell_manager(spell_error_object);
108 spell_error_object = 0;
113 void PSpell::cleanUp()
115 if (spell_error_object) {
116 delete_pspell_can_have_error(spell_error_object);
117 spell_error_object = 0;
122 enum PSpell::spellStatus PSpell::check(string const & word)
127 int word_ok = pspell_manager_check(sc, word.c_str());
128 lyx::Assert(word_ok != -1);
133 PspellWordList const * sugs =
134 pspell_manager_suggest(sc, word.c_str());
135 lyx::Assert(sugs != 0);
136 els = pspell_word_list_elements(sugs);
137 if (pspell_word_list_empty(sugs))
149 pspell_manager_save_all_word_lists(sc);
153 void PSpell::insert(string const & word)
156 pspell_manager_add_to_personal(sc, word.c_str());
160 void PSpell::accept(string const & word)
163 pspell_manager_add_to_session(sc, word.c_str());
167 void PSpell::store(string const & mis, string const & cor)
170 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
174 char const * PSpell::nextMiss()
177 return pspell_string_emulation_next(els);
182 char const * PSpell::error()
188 void PSpell::sigchldhandler(pid_t pid, int * status)
190 sigchldchecker(pid, status);
196 // ------------------- start special ispell code/class --------------------
200 : str(0), flag(ISP_UNKNOWN)
204 ISpell::ISpell(BufferParams const & params, string const & lang)
205 : str(0), flag(ISP_UNKNOWN)
207 initialize(params, lang);
217 char const * ISpell::nextMiss()
219 if (str == 0 || *(e+1) == '\0') return 0;
221 e = strpbrk(b, ",\n");
227 void ISpell::initialize(BufferParams const & params, string const & lang)
229 static char o_buf[BUFSIZ]; // jc: it could be smaller
237 if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
238 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
242 if ((out = fdopen(pipein[1], "w")) == 0) {
243 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
248 if ((in = fdopen(pipeout[0], "r")) == 0) {
249 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
254 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
261 lyxerr << "LyX: Can't create child process for spellchecker!"
268 dup2(pipein[0], STDIN_FILENO);
269 dup2(pipeout[1], STDOUT_FILENO);
276 char * tmp = new char[lyxrc.isp_command.length() + 1];
277 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
278 tmp[lyxrc.isp_command.length()] = '\0';
281 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
284 if (lang != "default") {
286 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
288 tmp = new char[lang.length() + 1];
289 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
293 if (lyxrc.isp_accept_compound) {
294 // Consider run-together words as legal compounds
296 string("-C").copy(tmp, 2); tmp[2] = '\0';
299 // Report run-together words with
300 // missing blanks as errors
302 string("-B").copy(tmp, 2); tmp[2] = '\0';
305 if (lyxrc.isp_use_esc_chars) {
306 // Specify additional characters that
307 // can be part of a word
309 string("-w").copy(tmp, 2); tmp[2] = '\0';
311 // Put the escape chars in ""s
312 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
313 tmp = new char[tms.length() + 1];
314 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
317 if (lyxrc.isp_use_pers_dict) {
318 // Specify an alternate personal dictionary
320 string("-p").copy(tmp, 2);
323 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
324 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
325 tmp[lyxrc.isp_pers_dict.length()] = '\0';
328 if (lyxrc.isp_use_input_encoding &&
329 params.inputenc != "default") {
330 string enc = (params.inputenc == "auto")
331 ? params.language->encoding()->LatexName()
333 string::size_type n = enc.length();
335 string("-T").copy(tmp, 2);
337 argv[argc++] = tmp; // Input encoding
338 tmp = new char[n + 1];
346 execvp(argv[0], const_cast<char * const *>(argv));
348 // free the memory used by string::copy in the
350 for (int i = 0; i < argc - 1; ++i)
353 lyxerr << "LyX: Failed to start ispell!" << endl;
357 /* Parent process: Read ispells identification message */
358 // Hmm...what are we using this id msg for? Nothing? (Lgb)
359 // Actually I used it to tell if it's truly Ispell or if it's
360 // aspell -- (kevinatk@home.com)
366 FD_SET(pipeout[0], &infds);
367 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
368 // but it can't really hurt.
371 // Configure provides us with macros which are supposed to do
372 // the right typecast.
373 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
374 SELECT_TYPE_ARG234 (&infds),
377 SELECT_TYPE_ARG5 (&tv));
380 // Ok, do the reading. We don't have to FD_ISSET since
381 // there is only one fd in infds.
382 fgets(buf, 2048, in);
384 // determine if the spell checker is really Aspell
385 if (strstr(buf, "Aspell"))
386 actual_spell_checker = ASC_ASPELL;
388 actual_spell_checker = ASC_ISPELL;
390 fputs("!\n", out); // Set terse mode (silently accept correct words)
393 } else if (retval == 0) {
394 // timeout. Give nice message to user.
395 lyxerr << "Ispell read timed out, what now?" << endl;
396 // This probably works but could need some thought
404 // Select returned error
405 lyxerr << "Select on ispell returned error, what now?" << endl;
412 "The ispell-process has died for some reason. *One* possible reason\n"
413 "could be that you do not have a dictionary file\n"
414 "for the language of this document installed.\n"
415 "Check /usr/lib/ispell or set another\n"
416 "dictionary in the Spellchecker Options menu.";
425 return isp_pid != -1;
429 void ISpell::cleanUp()
435 enum ISpell::spellStatus ISpell::check(string const & word)
437 //Please rewrite to use string.
439 ::fputs(word.c_str(), out);
443 ::fgets(buf, 1024, in);
445 // I think we have to check if ispell is still alive here because
446 // the signal-handler could have disabled blocking on the fd
447 if (!alive()) return ISP_UNKNOWN;
450 case '*': // Word found
453 case '+': // Word found through affix removal
456 case '-': // Word found through compound formation
457 flag = ISP_COMPOUNDWORD;
459 case '\n': // Number or when in terse mode: no problems
462 case '#': // Not found, no near misses and guesses
465 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
466 case '&': // Not found, but we have near misses
469 char * p = strpbrk(buf, ":");
470 str = new char[strlen(p) + 1];
475 default: // This shouldn't happend, but you know Murphy
480 if (flag!= ISP_IGNORE) {
481 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
489 // Note: If you decide to optimize this out when it is not
490 // needed please note that when Aspell is used this command
491 // is also needed to save the replacement dictionary.
492 // -- Kevin Atkinson (kevinatk@home.com)
494 fputs("#\n", out); // Save personal dictionary
501 void ISpell::insert(string const & word)
503 ::fputc('*', out); // Insert word in personal dictionary
504 ::fputs(word.c_str(), out);
509 void ISpell::accept(string const & word)
511 ::fputc('@', out); // Accept in this session
512 ::fputs(word.c_str(), out);
517 void ISpell::store(string const & mis, string const & cor)
519 if (actual_spell_checker == ASC_ASPELL) {
520 ::fputs("$$ra ", out);
521 ::fputs(mis.c_str(), out);
523 ::fputs(cor.c_str(), out);
529 void ISpell::sigchldhandler(pid_t pid, int * status)
532 if (pid == isp_pid) {
534 // set the file descriptor to nonblocking so we can
536 fcntl(isp_fd, F_SETFL, O_NONBLOCK);
539 sigchldchecker(pid, status);
543 char const * ISpell::error()