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