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
26 #ifdef HAVE_SYS_SELECT_H
27 # ifdef HAVE_STRINGS_H
28 // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
29 // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
34 #include <sys/select.h>
38 #include "support/lstrings.h"
39 #include "support/LAssert.h"
41 #define USE_ORIGINAL_MANAGER_FUNCS 1
42 # include <pspell/pspell.h>
44 #include "sp_pspell.h"
46 extern void sigchldchecker(pid_t pid, int *status);
53 spell_error_object = 0;
57 PSpell::PSpell(BufferParams const & params, string const & lang)
61 spell_error_object = 0;
63 initialize(params, lang);
71 delete_pspell_string_emulation(els);
75 void PSpell::initialize(BufferParams const &, string const & lang)
77 PspellConfig * config = new_pspell_config();
79 split(lang, code, '_');
80 config->replace("language-tag", code.c_str());
81 spell_error_object = new_pspell_manager(config);
82 if (pspell_error_number(spell_error_object) != 0) {
83 error_ = pspell_error_message(spell_error_object);
86 sc = to_pspell_manager(spell_error_object);
87 spell_error_object = 0;
92 void PSpell::cleanUp()
94 if (spell_error_object) {
95 delete_pspell_can_have_error(spell_error_object);
96 spell_error_object = 0;
101 enum PSpell::spellStatus PSpell::check(string const & word)
106 int word_ok = pspell_manager_check(sc, word.c_str());
107 lyx::Assert(word_ok != -1);
112 PspellWordList const * sugs =
113 pspell_manager_suggest(sc, word.c_str());
114 lyx::Assert(sugs != 0);
115 els = pspell_word_list_elements(sugs);
116 if (pspell_word_list_empty(sugs))
128 pspell_manager_save_all_word_lists(sc);
132 void PSpell::insert(string const & word)
135 pspell_manager_add_to_personal(sc, word.c_str());
139 void PSpell::accept(string const & word)
142 pspell_manager_add_to_session(sc, word.c_str());
146 void PSpell::store(string const & mis, string const & cor)
149 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
153 char const * PSpell::nextMiss()
156 return pspell_string_emulation_next(els);
159 char const * PSpell::error()
164 void PSpell::sigchldhandler(pid_t pid, int * status)
166 sigchldchecker(pid, status);
174 *This file is part of
175 * ======================================================
177 * LyX, The Document Processor
179 * Copyright 1995 Matthias Ettrich
180 * Copyright 1995-1998 The LyX Team
182 * ======================================================
189 #if TIME_WITH_SYS_TIME
190 # include <sys/time.h>
194 # include <sys/time.h>
200 #ifdef HAVE_SYS_SELECT_H
201 # ifdef HAVE_STRINGS_H
202 // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
203 // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
205 # include <strings.h>
208 #include <sys/select.h>
212 #include "support/lstrings.h"
215 #include "encoding.h"
216 #include "sp_ispell.h"
220 /// can be found in src/insets/figinset.C
221 extern void sigchldchecker(pid_t pid, int * status);
224 /// pid for the `ispell' process.
236 ISpell::ISpell(BufferParams const & params, string const & lang)
240 initialize(params, lang);
250 char const * ISpell::nextMiss()
252 if (str == 0 || *(e+1) == '\0') return 0;
254 e = strpbrk(b, ",\n");
260 void ISpell::initialize(BufferParams const & params, string const & lang)
262 static char o_buf[BUFSIZ]; // jc: it could be smaller
263 int pipein[2], pipeout[2];
269 if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
270 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
274 if ((out = fdopen(pipein[1], "w")) == 0) {
275 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
280 if ((in = fdopen(pipeout[0], "r")) == 0) {
281 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
286 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
293 lyxerr << "LyX: Can't create child process for spellchecker!"
300 dup2(pipein[0], STDIN_FILENO);
301 dup2(pipeout[1], STDOUT_FILENO);
308 char * tmp = new char[lyxrc.isp_command.length() + 1];
309 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
310 tmp[lyxrc.isp_command.length()] = '\0';
313 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
316 if (lang != "default") {
318 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
320 tmp = new char[lang.length() + 1];
321 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
325 if (lyxrc.isp_accept_compound) {
326 // Consider run-together words as legal compounds
328 string("-C").copy(tmp, 2); tmp[2] = '\0';
331 // Report run-together words with
332 // missing blanks as errors
334 string("-B").copy(tmp, 2); tmp[2] = '\0';
337 if (lyxrc.isp_use_esc_chars) {
338 // Specify additional characters that
339 // can be part of a word
341 string("-w").copy(tmp, 2); tmp[2] = '\0';
343 // Put the escape chars in ""s
344 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
345 tmp = new char[tms.length() + 1];
346 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
349 if (lyxrc.isp_use_pers_dict) {
350 // Specify an alternate personal dictionary
352 string("-p").copy(tmp, 2);
355 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
356 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
357 tmp[lyxrc.isp_pers_dict.length()] = '\0';
360 if (lyxrc.isp_use_input_encoding &&
361 params.inputenc != "default") {
362 string enc = (params.inputenc == "auto")
363 ? params.language->encoding()->LatexName()
365 string::size_type n = enc.length();
367 string("-T").copy(tmp, 2); tmp[2] = '\0';
368 argv[argc++] = tmp; // Input encoding
369 tmp = new char[n + 1];
377 execvp(argv[0], const_cast<char * const *>(argv));
379 // free the memory used by string::copy in the
381 for (int i= 0; i < argc -1; ++i)
384 lyxerr << "LyX: Failed to start ispell!" << endl;
388 /* Parent process: Read ispells identification message */
389 // Hmm...what are we using this id msg for? Nothing? (Lgb)
390 // Actually I used it to tell if it's truly Ispell or if it's
391 // aspell -- (kevinatk@home.com)
397 FD_SET(pipeout[0], &infds);
398 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
399 // but it can't really hurt.
402 // Configure provides us with macros which are supposed to do
403 // the right typecast.
404 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
405 SELECT_TYPE_ARG234 (&infds),
408 SELECT_TYPE_ARG5 (&tv));
411 // Ok, do the reading. We don't have to FD_ISSET since
412 // there is only one fd in infds.
413 fgets(buf, 2048, in);
415 // determine if the spell checker is really Aspell
416 if (strstr(buf, "Aspell"))
417 actual_spell_checker = ASC_ASPELL;
419 actual_spell_checker = ASC_ISPELL;
421 fputs("!\n", out); // Set terse mode (silently accept correct words)
424 } else if (retval == 0) {
425 // timeout. Give nice message to user.
426 lyxerr << "Ispell read timed out, what now?" << endl;
427 // This probably works but could need some thought
435 // Select returned error
436 lyxerr << "Select on ispell returned error, what now?" << endl;
443 "The ispell-process has died for some reason. *One* possible reason\n"
444 "could be that you do not have a dictionary file\n"
445 "for the language of this document installed.\n"
446 "Check /usr/lib/ispell or set another\n"
447 "dictionary in the Spellchecker Options menu.";
456 return isp_pid != -1;
460 void ISpell::cleanUp()
466 enum ISpell::spellStatus ISpell::check(string const & word)
468 //Please rewrite to use string.
470 ::fputs(word.c_str(), out);
474 ::fgets(buf, 1024, in);
476 /* I think we have to check if ispell is still alive here because
477 the signal-handler could have disabled blocking on the fd */
478 if (!alive()) return ISP_UNKNOWN;
481 case '*': // Word found
484 case '+': // Word found through affix removal
487 case '-': // Word found through compound formation
488 flag = ISP_COMPOUNDWORD;
490 case '\n': // Number or when in terse mode: no problems
493 case '#': // Not found, no near misses and guesses
496 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
497 case '&': // Not found, but we have near misses
500 char * p = strpbrk(buf, ":");
501 str = new char[strlen(p) + 1];
506 default: // This shouldn't happend, but you know Murphy
511 if (flag!= ISP_IGNORE) {
512 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
520 // Note: If you decide to optimize this out when it is not
521 // needed please note that when Aspell is used this command
522 // is also needed to save the replacement dictionary.
523 // -- Kevin Atkinson (kevinatk@home.com)
525 fputs("#\n", out); // Save personal dictionary
532 void ISpell::insert(string const & word)
534 ::fputc('*', out); // Insert word in personal dictionary
535 ::fputs(word.c_str(), out);
540 void ISpell::accept(string const & word)
542 ::fputc('@', out); // Accept in this session
543 ::fputs(word.c_str(), out);
548 void ISpell::store(string const & mis, string const & cor)
550 if (actual_spell_checker == ASC_ASPELL) {
551 ::fputs("$$ra ", out);
552 ::fputs(mis.c_str(), out);
554 ::fputs(cor.c_str(), out);
560 void ISpell::sigchldhandler(pid_t pid, int * status)
563 if (pid == isp_pid) {
565 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
566 to nonblocking so we can
570 sigchldchecker(pid, status);
573 char const * ISpell::error()