]> git.lyx.org Git - lyx.git/blob - src/sp_spell.C
add missing writeNormal() methods to some insets
[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           alive_(false)
79 {
80 }
81
82
83 PSpell::PSpell(BufferParams const & params, string const & lang)
84         : sc(0), els(0), spell_error_object(0), flag(ISP_UNKNOWN),
85           alive_(false)
86 {
87         initialize(params, lang);
88 }
89
90
91 PSpell::~PSpell() 
92 {
93         cleanUp();
94         close();
95         if (els)
96                 delete_pspell_string_emulation(els);
97 }
98         
99
100 void PSpell::initialize(BufferParams const &, string const & lang)
101 {
102         PspellConfig * config = new_pspell_config();
103         config->replace("language-tag", lang.c_str());
104         spell_error_object = new_pspell_manager(config);
105         if (pspell_error_number(spell_error_object) != 0) {
106                 error_ = pspell_error_message(spell_error_object);
107         } else {
108                 error_ = 0;
109                 sc = to_pspell_manager(spell_error_object);
110                 spell_error_object = 0;
111                 alive_ = true;
112         }
113 }
114
115
116 void PSpell::cleanUp() 
117 {
118         if (spell_error_object) {
119                 delete_pspell_can_have_error(spell_error_object);
120                 spell_error_object = 0;
121         }
122 }
123
124
125 enum PSpell::spellStatus PSpell::check(string const & word)
126 {
127         if (!sc)
128                 return flag;
129
130         int word_ok = pspell_manager_check(sc, word.c_str());
131         lyx::Assert(word_ok != -1);
132
133         if (word_ok) {
134                 flag = ISP_OK;
135         } else {
136                 PspellWordList const * sugs =
137                         pspell_manager_suggest(sc, word.c_str());
138                 lyx::Assert(sugs != 0);
139                 els = pspell_word_list_elements(sugs);
140                 if (pspell_word_list_empty(sugs)) 
141                         flag = ISP_UNKNOWN;
142                 else 
143                         flag = ISP_MISSED;
144         }
145         return flag;
146 }
147
148
149 void PSpell::close()
150 {
151         if (sc)
152                 pspell_manager_save_all_word_lists(sc);
153 }
154
155
156 void PSpell::insert(string const & word)
157 {
158         if (sc)
159                 pspell_manager_add_to_personal(sc, word.c_str());
160 }
161
162
163 void PSpell::accept(string const & word) 
164 {
165         if (sc)
166                 pspell_manager_add_to_session(sc, word.c_str());
167 }
168
169
170 void PSpell::store(string const & mis, string const & cor)
171 {
172         if (sc)
173                 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
174 }
175
176
177 char const * PSpell::nextMiss()
178 {
179         if (els)
180                 return pspell_string_emulation_next(els);
181         return "";
182 }
183
184
185 char const * PSpell::error()
186 {
187         return error_;
188 }
189
190
191 void PSpell::sigchldhandler(pid_t pid, int * status)
192 {
193         sigchldchecker(pid, status);
194 }
195
196 #endif
197
198 ///
199 // ------------------- start special ispell code/class --------------------
200 ///
201
202 ISpell::ISpell()
203         : str(0), flag(ISP_UNKNOWN)
204 {}
205
206
207 ISpell::ISpell(BufferParams const & params, string const & lang)
208         : str(0), flag(ISP_UNKNOWN)
209 {
210         initialize(params, lang);
211 }
212
213
214 ISpell::~ISpell()
215 {
216         delete[] str;
217 }
218
219
220 char const * ISpell::nextMiss()
221 {
222         if (str == 0 || *(e+1) == '\0') return 0;
223         b = e + 2;
224         e = strpbrk(b, ",\n");
225         *e = '\0';
226         return b;
227 }
228
229
230 void ISpell::initialize(BufferParams const & params, string const & lang)
231 {
232         static char o_buf[BUFSIZ];  // jc: it could be smaller
233         int pipein[2];
234         int pipeout[2];
235         char * argv[14];
236         int argc;
237
238         isp_pid = -1;
239
240         if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
241                 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
242                 goto END;
243         }
244
245         if ((out = fdopen(pipein[1], "w")) == 0) {
246                 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
247                        << endl;
248                 goto END;
249         }
250
251         if ((in = fdopen(pipeout[0], "r")) == 0) {
252                 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
253                        << endl;
254                 goto END;
255         }
256
257         setvbuf(out, o_buf, _IOLBF, BUFSIZ);
258
259         isp_fd = pipeout[0];
260
261         isp_pid = fork();
262
263         if (isp_pid == -1) {
264                 lyxerr << "LyX: Can't create child process for spellchecker!"
265                        << endl;
266                 goto END;
267         }
268         
269         if (isp_pid == 0) {        
270                 /* child process */
271                 dup2(pipein[0], STDIN_FILENO);
272                 dup2(pipeout[1], STDOUT_FILENO);
273                 ::close(pipein[0]);
274                 ::close(pipein[1]);
275                 ::close(pipeout[0]);
276                 ::close(pipeout[1]);
277
278                 argc = 0;
279                 char * tmp = new char[lyxrc.isp_command.length() + 1];
280                 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
281                 tmp[lyxrc.isp_command.length()] = '\0';
282                 argv[argc++] = tmp;
283                 tmp = new char[3];
284                 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
285                 argv[argc++] = tmp;
286
287                 if (lang != "default") {
288                         tmp = new char[3];
289                         string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
290                         argv[argc++] = tmp;
291                         tmp = new char[lang.length() + 1];
292                         lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
293                         argv[argc++] = tmp;
294                 }
295
296                 if (lyxrc.isp_accept_compound) {
297                         // Consider run-together words as legal compounds
298                         tmp = new char[3];
299                         string("-C").copy(tmp, 2); tmp[2] = '\0';
300                         argv[argc++] = tmp;
301                 } else {
302                         // Report run-together words with
303                         // missing blanks as errors
304                         tmp = new char[3]; 
305                         string("-B").copy(tmp, 2); tmp[2] = '\0';
306                         argv[argc++] = tmp; 
307                 }
308                 if (lyxrc.isp_use_esc_chars) {
309                         // Specify additional characters that
310                         // can be part of a word
311                         tmp = new char[3];
312                         string("-w").copy(tmp, 2); tmp[2] = '\0';
313                         argv[argc++] = tmp; 
314                         // Put the escape chars in ""s
315                         string tms = "\"" + lyxrc.isp_esc_chars + "\"";
316                         tmp = new char[tms.length() + 1];
317                         tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
318                         argv[argc++] = tmp;
319                 }
320                 if (lyxrc.isp_use_pers_dict) {
321                         // Specify an alternate personal dictionary
322                         tmp = new char[3];
323                         string("-p").copy(tmp, 2);
324                         tmp[2]= '\0';
325                         argv[argc++] = tmp;
326                         tmp = new char[lyxrc.isp_pers_dict.length() + 1];
327                         lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
328                         tmp[lyxrc.isp_pers_dict.length()] = '\0';
329                         argv[argc++] = tmp;
330                 }
331                 if (lyxrc.isp_use_input_encoding &&
332                     params.inputenc != "default") {
333                         string enc = (params.inputenc == "auto")
334                                 ? params.language->encoding()->LatexName()
335                                 : params.inputenc;
336                         string::size_type n = enc.length();
337                         tmp = new char[3];
338                         string("-T").copy(tmp, 2);
339                         tmp[2] = '\0';
340                         argv[argc++] = tmp; // Input encoding
341                         tmp = new char[n + 1];
342                         enc.copy(tmp, n);
343                         tmp[n] = '\0';
344                         argv[argc++] = tmp;
345                 }
346
347                 argv[argc++] = 0;
348
349                 execvp(argv[0], const_cast<char * const *>(argv));
350
351                 // free the memory used by string::copy in the
352                 // setup of argv
353                 for (int i = 0; i < argc - 1; ++i)
354                         delete[] argv[i];
355                 
356                 lyxerr << "LyX: Failed to start ispell!" << endl;
357                 _exit(0);
358         }
359         {
360                 /* Parent process: Read ispells identification message */
361                 // Hmm...what are we using this id msg for? Nothing? (Lgb)
362                 // Actually I used it to tell if it's truly Ispell or if it's
363                 // aspell -- (kevinatk@home.com)
364                 char buf[2048];
365                 fd_set infds;
366                 struct timeval tv;
367                 int retval = 0;
368                 FD_ZERO(&infds);
369                 FD_SET(pipeout[0], &infds);
370                 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
371                 // but it can't really hurt.
372                 tv.tv_usec = 0;
373
374                 // Configure provides us with macros which are supposed to do
375                 // the right typecast.
376                 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1), 
377                                 SELECT_TYPE_ARG234 (&infds), 
378                                 0, 
379                                 0, 
380                                 SELECT_TYPE_ARG5 (&tv));
381
382                 if (retval > 0) {
383                         // Ok, do the reading. We don't have to FD_ISSET since
384                         // there is only one fd in infds.
385                         fgets(buf, 2048, in);
386                 
387                         // determine if the spell checker is really Aspell
388                         if (strstr(buf, "Aspell"))
389                                 actual_spell_checker = ASC_ASPELL;
390                         else
391                                 actual_spell_checker = ASC_ISPELL;
392
393                         fputs("!\n", out); // Set terse mode (silently accept correct words)
394
395                 
396                 } else if (retval == 0) {
397                         // timeout. Give nice message to user.
398                         lyxerr << "Ispell read timed out, what now?" << endl;
399                         // This probably works but could need some thought
400                         isp_pid = -1;
401                         ::close(pipeout[0]);
402                         ::close(pipeout[1]);
403                         ::close(pipein[0]);
404                         ::close(pipein[1]);
405                         isp_fd = -1;
406                 } else {
407                         // Select returned error
408                         lyxerr << "Select on ispell returned error, what now?" << endl;
409                 }
410         }
411   END:
412         if (isp_pid == -1) {
413                 error_ = 
414                         "\n\n"
415                         "The ispell-process has died for some reason. *One* possible reason\n"
416                         "could be that you do not have a dictionary file\n"
417                         "for the language of this document installed.\n"
418                         "Check /usr/lib/ispell or set another\n"
419                         "dictionary in the Spellchecker Options menu.";
420         } else {
421                 error_ = 0;
422         }
423 }
424
425
426 bool ISpell::alive()
427 {
428         return isp_pid != -1;
429 }
430
431
432 void ISpell::cleanUp() 
433 {
434         ::fclose(out);
435 }
436
437
438 enum ISpell::spellStatus ISpell::check(string const & word)
439 {
440         //Please rewrite to use string.
441
442         ::fputs(word.c_str(), out);
443         ::fputc('\n', out);
444   
445         char buf[1024];
446         ::fgets(buf, 1024, in); 
447   
448         // I think we have to check if ispell is still alive here because
449         // the signal-handler could have disabled blocking on the fd 
450         if (!alive()) return ISP_UNKNOWN;
451
452         switch (*buf) {
453         case '*': // Word found
454                 flag = ISP_OK;
455                 break;
456         case '+': // Word found through affix removal
457                 flag = ISP_ROOT;
458                 break;
459         case '-': // Word found through compound formation
460                 flag = ISP_COMPOUNDWORD;
461                 break;
462         case '\n': // Number or when in terse mode: no problems
463                 flag = ISP_IGNORE;
464                 break;
465         case '#': // Not found, no near misses and guesses
466                 flag = ISP_UNKNOWN;
467                 break;
468         case '?': // Not found, and no near misses, but guesses (guesses are ignored)
469         case '&': // Not found, but we have near misses
470         {
471                 flag = ISP_MISSED;
472                 char * p = strpbrk(buf, ":");
473                 str = new char[strlen(p) + 1];
474                 e   = str;
475                 strcpy(str, p);
476                 break;
477         }
478         default: // This shouldn't happend, but you know Murphy
479                 flag = ISP_UNKNOWN;
480         }
481
482         *buf = 0;
483         if (flag!= ISP_IGNORE) {
484                 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
485         }
486         return flag;
487 }
488
489
490 void ISpell::close()
491 {
492         // Note: If you decide to optimize this out when it is not 
493         // needed please note that when Aspell is used this command 
494         // is also needed to save the replacement dictionary.
495         // -- Kevin Atkinson (kevinatk@home.com)
496
497         fputs("#\n", out); // Save personal dictionary
498
499         fflush(out);
500         fclose(out);
501 }
502
503
504 void ISpell::insert(string const & word)
505 {
506         ::fputc('*', out); // Insert word in personal dictionary
507         ::fputs(word.c_str(), out);
508         ::fputc('\n', out);
509 }
510
511
512 void ISpell::accept(string const & word) 
513 {
514         ::fputc('@', out); // Accept in this session
515         ::fputs(word.c_str(), out);
516         ::fputc('\n', out);
517 }
518
519
520 void ISpell::store(string const & mis, string const & cor)
521 {
522         if (actual_spell_checker == ASC_ASPELL) {
523                 ::fputs("$$ra ", out);
524                 ::fputs(mis.c_str(), out);
525                 ::fputc(',', out);
526                 ::fputs(cor.c_str(), out);
527                 ::fputc('\n', out);
528         }
529 }
530
531
532 void ISpell::sigchldhandler(pid_t pid, int * status)
533 {
534         if (isp_pid > 0) {
535                 if (pid == isp_pid) {
536                         isp_pid = -1;
537                         // set the file descriptor to nonblocking so we can
538                         // continue 
539                         fcntl(isp_fd, F_SETFL, O_NONBLOCK);
540                 }
541         }
542         sigchldchecker(pid, status);
543 }
544
545
546 char const * ISpell::error()
547 {
548         return error_;
549 }