]> git.lyx.org Git - lyx.git/blob - src/sp_spell.C
ce6323cfaa8b0bff415c390cdd6bb5742770d401
[lyx.git] / src / sp_spell.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file sp_pspell.C
11  * \author Kevin Atkinson
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <ctime>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <ctime>
32 # endif
33 #endif
34
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 :(
39 #  ifdef _AIX
40 #   include <strings.h>
41 #  endif
42 # endif
43 #include <sys/select.h>
44 #endif
45
46 #include "LString.h"
47 #include "support/lstrings.h"
48 #include "lyxrc.h"
49 #include "debug.h"
50 #include "encoding.h"
51 #include "sp_ispell.h"
52
53 using std::endl;
54
55 namespace {
56         /// pid for the `ispell' process. 
57         pid_t isp_pid = -1; 
58 }
59
60 /// can be found in src/insets/figinset.C
61 extern void sigchldchecker(pid_t pid, int * status);
62
63 ///
64 // ------------------- start special pspell code/class --------------------
65 ///
66 #ifdef USE_PSPELL
67
68 #include "support/LAssert.h"
69
70 #define USE_ORIGINAL_MANAGER_FUNCS 1
71 # include <pspell/pspell.h>
72
73 #include "sp_pspell.h"
74
75
76 PSpell::PSpell() 
77         : sc(0), els(0), spell_error_object(0), flag(ISP_UNKNOWN)
78 {
79 }
80
81
82 PSpell::PSpell(BufferParams const & params, string const & lang)
83         : sc(0), els(0), spell_error_object(0), flag(ISP_UNKNOWN)
84 {
85         initialize(params, lang);
86 }
87
88
89 PSpell::~PSpell() 
90 {
91         cleanUp();
92         close();
93         if (els)
94                 delete_pspell_string_emulation(els);
95 }
96         
97
98 void PSpell::initialize(BufferParams const &, string const & lang)
99 {
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);
105         } else {
106                 error_ = 0;
107                 sc = to_pspell_manager(spell_error_object);
108                 spell_error_object = 0;
109         }
110 }
111
112
113 void PSpell::cleanUp() 
114 {
115         if (spell_error_object) {
116                 delete_pspell_can_have_error(spell_error_object);
117                 spell_error_object = 0;
118         }
119 }
120
121
122 enum PSpell::spellStatus PSpell::check(string const & word)
123 {
124         if (!sc)
125                 return flag;
126
127         int word_ok = pspell_manager_check(sc, word.c_str());
128         lyx::Assert(word_ok != -1);
129
130         if (word_ok) {
131                 flag = ISP_OK;
132         } else {
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)) 
138                         flag = ISP_UNKNOWN;
139                 else 
140                         flag = ISP_MISSED;
141         }
142         return flag;
143 }
144
145
146 void PSpell::close()
147 {
148         if (sc)
149                 pspell_manager_save_all_word_lists(sc);
150 }
151
152
153 void PSpell::insert(string const & word)
154 {
155         if (sc)
156                 pspell_manager_add_to_personal(sc, word.c_str());
157 }
158
159
160 void PSpell::accept(string const & word) 
161 {
162         if (sc)
163                 pspell_manager_add_to_session(sc, word.c_str());
164 }
165
166
167 void PSpell::store(string const & mis, string const & cor)
168 {
169         if (sc)
170                 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
171 }
172
173
174 char const * PSpell::nextMiss()
175 {
176         if (els)
177                 return pspell_string_emulation_next(els);
178         return "";
179 }
180
181
182 char const * PSpell::error()
183 {
184         return error_;
185 }
186
187
188 void PSpell::sigchldhandler(pid_t pid, int * status)
189 {
190         sigchldchecker(pid, status);
191 }
192
193 #endif
194
195 ///
196 // ------------------- start special ispell code/class --------------------
197 ///
198
199 ISpell::ISpell()
200         : str(0), flag(ISP_UNKNOWN)
201 {}
202
203
204 ISpell::ISpell(BufferParams const & params, string const & lang)
205         : str(0), flag(ISP_UNKNOWN)
206 {
207         initialize(params, lang);
208 }
209
210
211 ISpell::~ISpell()
212 {
213         delete[] str;
214 }
215
216
217 char const * ISpell::nextMiss()
218 {
219         if (str == 0 || *(e+1) == '\0') return 0;
220         b = e + 2;
221         e = strpbrk(b, ",\n");
222         *e = '\0';
223         return b;
224 }
225
226
227 void ISpell::initialize(BufferParams const & params, string const & lang)
228 {
229         static char o_buf[BUFSIZ];  // jc: it could be smaller
230         int pipein[2];
231         int pipeout[2];
232         char * argv[14];
233         int argc;
234
235         isp_pid = -1;
236
237         if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
238                 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
239                 goto END;
240         }
241
242         if ((out = fdopen(pipein[1], "w")) == 0) {
243                 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
244                        << endl;
245                 goto END;
246         }
247
248         if ((in = fdopen(pipeout[0], "r")) == 0) {
249                 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
250                        << endl;
251                 goto END;
252         }
253
254         setvbuf(out, o_buf, _IOLBF, BUFSIZ);
255
256         isp_fd = pipeout[0];
257
258         isp_pid = fork();
259
260         if (isp_pid == -1) {
261                 lyxerr << "LyX: Can't create child process for spellchecker!"
262                        << endl;
263                 goto END;
264         }
265         
266         if (isp_pid == 0) {        
267                 /* child process */
268                 dup2(pipein[0], STDIN_FILENO);
269                 dup2(pipeout[1], STDOUT_FILENO);
270                 ::close(pipein[0]);
271                 ::close(pipein[1]);
272                 ::close(pipeout[0]);
273                 ::close(pipeout[1]);
274
275                 argc = 0;
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';
279                 argv[argc++] = tmp;
280                 tmp = new char[3];
281                 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
282                 argv[argc++] = tmp;
283
284                 if (lang != "default") {
285                         tmp = new char[3];
286                         string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
287                         argv[argc++] = tmp;
288                         tmp = new char[lang.length() + 1];
289                         lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
290                         argv[argc++] = tmp;
291                 }
292
293                 if (lyxrc.isp_accept_compound) {
294                         // Consider run-together words as legal compounds
295                         tmp = new char[3];
296                         string("-C").copy(tmp, 2); tmp[2] = '\0';
297                         argv[argc++] = tmp;
298                 } else {
299                         // Report run-together words with
300                         // missing blanks as errors
301                         tmp = new char[3]; 
302                         string("-B").copy(tmp, 2); tmp[2] = '\0';
303                         argv[argc++] = tmp; 
304                 }
305                 if (lyxrc.isp_use_esc_chars) {
306                         // Specify additional characters that
307                         // can be part of a word
308                         tmp = new char[3];
309                         string("-w").copy(tmp, 2); tmp[2] = '\0';
310                         argv[argc++] = tmp; 
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';
315                         argv[argc++] = tmp;
316                 }
317                 if (lyxrc.isp_use_pers_dict) {
318                         // Specify an alternate personal dictionary
319                         tmp = new char[3];
320                         string("-p").copy(tmp, 2);
321                         tmp[2]= '\0';
322                         argv[argc++] = tmp;
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';
326                         argv[argc++] = tmp;
327                 }
328                 if (lyxrc.isp_use_input_encoding &&
329                     params.inputenc != "default") {
330                         string enc = (params.inputenc == "auto")
331                                 ? params.language->encoding()->LatexName()
332                                 : params.inputenc;
333                         string::size_type n = enc.length();
334                         tmp = new char[3];
335                         string("-T").copy(tmp, 2);
336                         tmp[2] = '\0';
337                         argv[argc++] = tmp; // Input encoding
338                         tmp = new char[n + 1];
339                         enc.copy(tmp, n);
340                         tmp[n] = '\0';
341                         argv[argc++] = tmp;
342                 }
343
344                 argv[argc++] = 0;
345
346                 execvp(argv[0], const_cast<char * const *>(argv));
347
348                 // free the memory used by string::copy in the
349                 // setup of argv
350                 for (int i = 0; i < argc - 1; ++i)
351                         delete[] argv[i];
352                 
353                 lyxerr << "LyX: Failed to start ispell!" << endl;
354                 _exit(0);
355         }
356         {
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)
361                 char buf[2048];
362                 fd_set infds;
363                 struct timeval tv;
364                 int retval = 0;
365                 FD_ZERO(&infds);
366                 FD_SET(pipeout[0], &infds);
367                 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
368                 // but it can't really hurt.
369                 tv.tv_usec = 0;
370
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), 
375                                 0, 
376                                 0, 
377                                 SELECT_TYPE_ARG5 (&tv));
378
379                 if (retval > 0) {
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);
383                 
384                         // determine if the spell checker is really Aspell
385                         if (strstr(buf, "Aspell"))
386                                 actual_spell_checker = ASC_ASPELL;
387                         else
388                                 actual_spell_checker = ASC_ISPELL;
389
390                         fputs("!\n", out); // Set terse mode (silently accept correct words)
391
392                 
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
397                         isp_pid = -1;
398                         ::close(pipeout[0]);
399                         ::close(pipeout[1]);
400                         ::close(pipein[0]);
401                         ::close(pipein[1]);
402                         isp_fd = -1;
403                 } else {
404                         // Select returned error
405                         lyxerr << "Select on ispell returned error, what now?" << endl;
406                 }
407         }
408   END:
409         if (isp_pid == -1) {
410                 error_ = 
411                         "\n\n"
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.";
417         } else {
418                 error_ = 0;
419         }
420 }
421
422
423 bool ISpell::alive()
424 {
425         return isp_pid != -1;
426 }
427
428
429 void ISpell::cleanUp() 
430 {
431         ::fclose(out);
432 }
433
434
435 enum ISpell::spellStatus ISpell::check(string const & word)
436 {
437         //Please rewrite to use string.
438
439         ::fputs(word.c_str(), out);
440         ::fputc('\n', out);
441   
442         char buf[1024];
443         ::fgets(buf, 1024, in); 
444   
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;
448
449         switch (*buf) {
450         case '*': // Word found
451                 flag = ISP_OK;
452                 break;
453         case '+': // Word found through affix removal
454                 flag = ISP_ROOT;
455                 break;
456         case '-': // Word found through compound formation
457                 flag = ISP_COMPOUNDWORD;
458                 break;
459         case '\n': // Number or when in terse mode: no problems
460                 flag = ISP_IGNORE;
461                 break;
462         case '#': // Not found, no near misses and guesses
463                 flag = ISP_UNKNOWN;
464                 break;
465         case '?': // Not found, and no near misses, but guesses (guesses are ignored)
466         case '&': // Not found, but we have near misses
467         {
468                 flag = ISP_MISSED;
469                 char * p = strpbrk(buf, ":");
470                 str = new char[strlen(p) + 1];
471                 e   = str;
472                 strcpy(str, p);
473                 break;
474         }
475         default: // This shouldn't happend, but you know Murphy
476                 flag = ISP_UNKNOWN;
477         }
478
479         *buf = 0;
480         if (flag!= ISP_IGNORE) {
481                 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
482         }
483         return flag;
484 }
485
486
487 void ISpell::close()
488 {
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)
493
494         fputs("#\n", out); // Save personal dictionary
495
496         fflush(out);
497         fclose(out);
498 }
499
500
501 void ISpell::insert(string const & word)
502 {
503         ::fputc('*', out); // Insert word in personal dictionary
504         ::fputs(word.c_str(), out);
505         ::fputc('\n', out);
506 }
507
508
509 void ISpell::accept(string const & word) 
510 {
511         ::fputc('@', out); // Accept in this session
512         ::fputs(word.c_str(), out);
513         ::fputc('\n', out);
514 }
515
516
517 void ISpell::store(string const & mis, string const & cor)
518 {
519         if (actual_spell_checker == ASC_ASPELL) {
520                 ::fputs("$$ra ", out);
521                 ::fputs(mis.c_str(), out);
522                 ::fputc(',', out);
523                 ::fputs(cor.c_str(), out);
524                 ::fputc('\n', out);
525         }
526 }
527
528
529 void ISpell::sigchldhandler(pid_t pid, int * status)
530 {
531         if (isp_pid > 0) {
532                 if (pid == isp_pid) {
533                         isp_pid = -1;
534                         // set the file descriptor to nonblocking so we can
535                         // continue 
536                         fcntl(isp_fd, F_SETFL, O_NONBLOCK);
537                 }
538         }
539         sigchldchecker(pid, status);
540 }
541
542
543 char const * ISpell::error()
544 {
545         return error_;
546 }