1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * lookup.c - see if a word appears in the dictionary
7 * Copyright 1987, 1988, 1989, 1992, 1993, Geoff Kuenning, Granada Hills, CA
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All modifications to the source code must be clearly marked as
20 * such. Binary redistributions based on modified source code
21 * must be clearly marked as modified versions in the documentation
22 * and/or other materials provided with the distribution.
23 * 4. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgment:
25 * This product includes software developed by Geoff Kuenning and
26 * other unpaid contributors.
27 * 5. The name of Geoff Kuenning may not be used to endorse or promote
28 * products derived from this software without specific prior
31 * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * Revision 1.7 2003/09/25 02:44:48 dom
49 * Revision 1.6 2003/08/26 13:20:40 dom
50 * ispell crasher fix, implement enchant_dictionary_release
52 * Revision 1.5 2003/08/26 13:08:03 uwog
53 * Fix segfault when the requested dictionary couldn't be found.
55 * Revision 1.4 2003/08/14 16:27:36 dom
56 * update some documentation
58 * Revision 1.3 2003/07/28 20:40:27 dom
59 * fix up the license clause, further win32-registry proof some directory getting functions
61 * Revision 1.2 2003/07/16 22:52:47 dom
62 * LGPL + exception license
64 * Revision 1.1 2003/07/15 01:15:07 dom
65 * ispell enchant backend
67 * Revision 1.3 2003/01/29 05:50:12 hippietrail
69 * Fixed my mess in EncodingManager.
70 * Changed many C casts to C++ casts.
72 * Revision 1.2 2003/01/25 03:16:05 hippietrail
74 * An UT_ICONV_INVALID fix which escaped the last commit.
76 * Revision 1.1 2003/01/24 05:52:34 hippietrail
78 * Refactored ispell code. Old ispell global variables had been put into
79 * an allocated structure, a pointer to which was passed to many functions.
80 * I have now made all such functions and variables private members of the
81 * ISpellChecker class. It was C OO, now it's C++ OO.
83 * I've fixed the makefiles and tested compilation but am unable to test
84 * operation. Please back out my changes if they cause problems which
85 * are not obvious or easy to fix.
87 * Revision 1.12 2003/01/06 18:48:39 dom
88 * ispell cleanup, start of using new 'add' save features
90 * Revision 1.11 2002/09/19 05:31:17 hippietrail
92 * More Ispell cleanup. Conditional globals and DEREF macros are removed.
93 * K&R function declarations removed, converted to Doxygen style comments
94 * where possible. No code has been changed (I hope). Compiles for me but
97 * Revision 1.10 2002/09/17 03:03:30 hippietrail
99 * After seeking permission on the developer list I've reformatted all the
100 * spelling source which seemed to have parts which used 2, 3, 4, and 8
101 * spaces for tabs. It should all look good with our standard 4-space
103 * I've concentrated just on indentation in the actual code. More prettying
105 * * NO code changes were made *
107 * Revision 1.9 2002/09/13 17:20:13 mpritchett
108 * Fix more warnings for Linux build
110 * Revision 1.8 2002/05/03 09:49:43 fjfranklin
111 * o hash downloader update (Gabriel Gerhardsson)
112 * - Comment out the "Can't open <dictionary>" printf.
113 * - Make the progressbar more clean at the begining of the download.
114 * - Add support for tarballs that doesn't have the full path included
115 * - Fix copyright headers on the newly added files (*HashDownloader.*)
117 * Revision 1.7 2001/08/27 19:06:30 dom
118 * Lots of compilation fixes
120 * Revision 1.6 2001/08/10 18:32:40 dom
121 * Spelling and iconv updates. god, i hate iconv
123 * Revision 1.5 2001/08/10 09:57:49 hub
124 * Patch by sobomax@FreeBSD.org
125 * #include "iconv.h" directive is missed from src/other/spell/xp/lookup.c and
126 * src/wp/impexp/xp/ie_imp_RTF.cpp.
129 * Revision 1.4 2001/07/18 17:46:01 dom
130 * Module changes, and fix compiler warnings
132 * Revision 1.3 2001/06/12 21:32:49 dom
133 * More ispell work...
135 * Revision 1.2 2001/05/12 16:05:42 thomasf
136 * Big pseudo changes to ispell to make it pass around a structure rather
137 * than rely on all sorts of gloabals willy nilly here and there. Also
138 * fixed our spelling class to work with accepting suggestions once more.
139 * This code is dirty, gross and ugly (not to mention still not supporting
140 * multiple hash sized just yet) but it works on my machine and will no
141 * doubt break other machines.
143 * Revision 1.1 2001/04/15 16:01:24 tomas_f
146 * Revision 1.7 1999/09/29 23:33:32 justin
147 * Updates to the underlying ispell-based code to support suggested corrections.
149 * Revision 1.6 1999/04/13 17:12:51 jeff
150 * Applied "Darren O. Benham" <gecko@benham.net> spell check changes.
151 * Fixed crash on Win32 with the new code.
153 * Revision 1.5 1999/01/07 01:07:48 paul
156 * Revision 1.5 1999/01/07 01:07:48 paul
159 * Revision 1.4 1998/12/29 14:55:33 eric
161 * I've doctored the ispell code pretty extensively here. It is now
162 * warning-free on Win32. It also *works* on Win32 now, since I
163 * replaced all the I/O calls with ANSI standard ones.
165 * Revision 1.3 1998/12/28 23:11:30 eric
167 * modified spell code and integration to build on Windows.
168 * This is still a hack.
170 * Actually, it doesn't yet WORK on Windows. It just builds.
171 * SpellCheckInit is failing for some reason.
173 * Revision 1.2 1998/12/28 22:16:22 eric
175 * These changes begin to incorporate the spell checker into AbiWord. Most
178 * 1. added other/spell to the -I list in config/abi_defs
179 * 2. replaced other/spell/Makefile with one which is more like
181 * 3. added other/spell to other/Makefile so that the build will now
182 * dive down and build the spell check library.
183 * 4. added the AbiSpell library to the Makefiles in wp/main
184 * 5. added a call to SpellCheckInit in wp/main/unix/UnixMain.cpp.
185 * This call is a HACK and should be replaced with something
187 * 6. added code to fv_View.cpp as follows:
188 * whenever you double-click on a word, the spell checker
189 * verifies that word and prints its status to stdout.
192 * 1. This will break the Windows build. I'm going to work on fixing it
194 * 2. This only works if your dictionary is in /usr/lib/ispell/american.hash.
195 * The dictionary location is currently hard-coded. This will be
198 * Anyway, such as it is, it works.
200 * Revision 1.1 1998/12/28 18:04:43 davet
201 * Spell checker code stripped from ispell. At this point, there are
202 * two external routines... the Init routine, and a check-a-word routine
203 * which returns a boolean value, and takes a 16 bit char string.
204 * The code resembles the ispell code as much as possible still.
206 * Revision 1.42 1995/01/08 23:23:42 geoff
207 * Support MSDOS_BINARY_OPEN when opening the hash file to read it in.
209 * Revision 1.41 1994/01/25 07:11:51 geoff
210 * Get rid of all old RCS log lines in preparation for the 3.1 release.
218 #include "enchant-provider.h"
219 #include "ispell_checker.h"
222 #define G_ICONV_INVALID (GIConv)-1
224 static bool g_iconv_is_valid(GIConv i)
226 return (i != G_ICONV_INVALID);
230 static void dumpindex P ((struct flagptr * indexp, int depth));
231 #endif /* INDEXDUMP */
236 * \param hashname name of the hash file (dictionary)
240 int ISpellChecker::linit (char *hashname)
245 register struct dent * dp;
246 struct flagent * entry;
247 struct flagptr * ind;
250 register ichar_t * cp;
252 if ((fpHash = enchant_fopen (hashname, "rb")) == NULL)
257 m_hashsize = fread (reinterpret_cast<char *>(&m_hashheader), 1, sizeof m_hashheader, fpHash);
258 if (m_hashsize < static_cast<int>(sizeof(m_hashheader)))
261 fprintf (stderr, LOOKUP_C_CANT_READ, hashname);
262 else if (m_hashsize == 0)
263 fprintf (stderr, LOOKUP_C_NULL_HASH, hashname);
266 LOOKUP_C_SHORT_HASH (m_hashname, m_hashsize,
267 static_cast<int>(sizeof m_hashheader)));
270 else if (m_hashheader.magic != MAGIC)
273 LOOKUP_C_BAD_MAGIC (hashname, static_cast<unsigned int>(MAGIC),
274 static_cast<unsigned int>(m_hashheader.magic)));
277 else if (m_hashheader.magic2 != MAGIC)
280 LOOKUP_C_BAD_MAGIC2 (hashname, static_cast<unsigned int>(MAGIC),
281 static_cast<unsigned int>(m_hashheader.magic2)));
284 /* else if (hashheader.compileoptions != COMPILEOPTIONS*/
286 || m_hashheader.maxstringchars != MAXSTRINGCHARS
287 || m_hashheader.maxstringcharlen != MAXSTRINGCHARLEN)
290 LOOKUP_C_BAD_OPTIONS (static_cast<unsigned int>(m_hashheader.compileoptions),
291 m_hashheader.maxstringchars, m_hashheader.maxstringcharlen,
292 static_cast<unsigned int>(COMPILEOPTIONS), MAXSTRINGCHARS, MAXSTRINGCHARLEN));
299 calloc (static_cast<unsigned>(m_hashheader.tblsize), sizeof (struct dent));
300 m_hashsize = m_hashheader.tblsize;
301 m_hashstrings = static_cast<char *>(malloc(static_cast<unsigned>(m_hashheader.stringsize)));
303 m_numsflags = m_hashheader.stblsize;
304 m_numpflags = m_hashheader.ptblsize;
305 m_sflaglist = (struct flagent *)
306 malloc ((m_numsflags + m_numpflags) * sizeof (struct flagent));
307 if (m_hashtbl == NULL || m_hashstrings == NULL || m_sflaglist == NULL)
309 fprintf (stderr, LOOKUP_C_NO_HASH_SPACE);
312 m_pflaglist = m_sflaglist + m_numsflags;
315 if( fread ( m_hashstrings, 1, static_cast<unsigned>(m_hashheader.stringsize), fpHash)
316 != static_cast<size_t>(m_hashheader.stringsize) )
318 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
319 fprintf (stderr, "stringsize err\n" );
322 if ( m_hashheader.compileoptions & 0x04 )
324 if( fread (reinterpret_cast<char *>(m_hashtbl), 1, static_cast<unsigned>(m_hashheader.tblsize) * sizeof(struct dent), fpHash)
325 != (static_cast<size_t>(m_hashheader.tblsize * sizeof (struct dent))))
327 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
333 for( x=0; x<m_hashheader.tblsize; x++ )
335 if( fread ( reinterpret_cast<char*>(m_hashtbl+x), sizeof( struct dent)-sizeof( MASKTYPE ), 1, fpHash)
338 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
344 if (fread (reinterpret_cast<char *>(m_sflaglist), 1,
345 static_cast<unsigned>(m_numsflags+ m_numpflags) * sizeof (struct flagent), fpHash)
346 != (m_numsflags + m_numpflags) * sizeof (struct flagent))
348 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
354 for (i = m_hashsize, dp = m_hashtbl; --i >= 0; dp++)
356 if (dp->word == (char *) -1)
359 dp->word = &m_hashstrings [ reinterpret_cast<size_t>(dp->word) ];
360 if (dp->next == (struct dent *) -1)
363 dp->next = &m_hashtbl [ reinterpret_cast<size_t>(dp->next) ];
367 for (i = m_numsflags + m_numpflags, entry = m_sflaglist; --i >= 0; entry++)
370 entry->strip = reinterpret_cast<ichar_t *>(&m_hashstrings[reinterpret_cast<size_t>(entry->strip)]);
374 entry->affix = reinterpret_cast<ichar_t *>(&m_hashstrings[reinterpret_cast<size_t>(entry->affix)]);
379 ** Warning - 'entry' and 'i' are reset in the body of the loop
380 ** below. Don't try to optimize it by (e.g.) moving the decrement
381 ** of i into the loop condition.
383 for (i = m_numsflags, entry = m_sflaglist; i > 0; i--, entry++)
385 if (entry->affl == 0)
388 ind = &m_sflagindex[0];
393 cp = entry->affix + entry->affl - 1;
394 ind = &m_sflagindex[*cp];
396 while (ind->numents == 0 && ind->pu.fp != NULL)
398 if (cp == entry->affix)
400 ind = &ind->pu.fp[0];
405 ind = &ind->pu.fp[*--cp];
410 if (ind->numents == 0)
414 ** If this index entry has more than MAXSEARCH flags in
415 ** it, we will split it into subentries to reduce the
416 ** searching. However, the split doesn't make sense in
417 ** two cases: (a) if we are already at the end of the
418 ** current affix, or (b) if all the entries in the list
419 ** have identical affixes. Since the list is sorted, (b)
420 ** is true if the first and last affixes in the list
423 if (!viazero && ind->numents >= MAXSEARCH
424 && icharcmp (entry->affix, ind->pu.ent->affix) != 0)
426 /* Sneaky trick: back up and reprocess */
427 entry = ind->pu.ent - 1; /* -1 is for entry++ in loop */
428 i = m_numsflags - (entry - m_sflaglist);
431 calloc (static_cast<unsigned>(SET_SIZE + m_hashheader.nstrchars),
432 sizeof (struct flagptr));
433 if (ind->pu.fp == NULL)
435 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
442 ** Warning - 'entry' and 'i' are reset in the body of the loop
443 ** below. Don't try to optimize it by (e.g.) moving the decrement
444 ** of i into the loop condition.
446 for (i = m_numpflags, entry = m_pflaglist; i > 0; i--, entry++)
448 if (entry->affl == 0)
451 ind = &m_pflagindex[0];
457 ind = &m_pflagindex[*cp++];
459 while (ind->numents == 0 && ind->pu.fp != NULL)
463 ind = &ind->pu.fp[0];
468 ind = &ind->pu.fp[*cp++];
473 if (ind->numents == 0)
477 ** If this index entry has more than MAXSEARCH flags in
478 ** it, we will split it into subentries to reduce the
479 ** searching. However, the split doesn't make sense in
480 ** two cases: (a) if we are already at the end of the
481 ** current affix, or (b) if all the entries in the list
482 ** have identical affixes. Since the list is sorted, (b)
483 ** is true if the first and last affixes in the list
486 if (!viazero && ind->numents >= MAXSEARCH
487 && icharcmp (entry->affix, ind->pu.ent->affix) != 0)
489 /* Sneaky trick: back up and reprocess */
490 entry = ind->pu.ent - 1; /* -1 is for entry++ in loop */
491 i = m_numpflags - (entry - m_pflaglist);
493 static_cast<struct flagptr *>(calloc(SET_SIZE + m_hashheader.nstrchars,
494 sizeof (struct flagptr)));
495 if (ind->pu.fp == NULL)
497 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
504 fprintf (stderr, "Prefix index table:\n");
505 dumpindex (m_pflagindex, 0);
506 fprintf (stderr, "Suffix index table:\n");
507 dumpindex (m_sflagindex, 0);
509 if (m_hashheader.nstrchartype == 0)
513 m_chartypes = (struct strchartype *)
514 malloc (m_hashheader.nstrchartype * sizeof (struct strchartype));
515 if (m_chartypes == NULL)
517 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
520 for (i = 0, nextchar = m_hashheader.strtypestart;
521 i < m_hashheader.nstrchartype;
524 m_chartypes[i].name = &m_hashstrings[nextchar];
525 nextchar += strlen (m_chartypes[i].name) + 1;
526 m_chartypes[i].deformatter = &m_hashstrings[nextchar];
527 nextchar += strlen (m_chartypes[i].deformatter) + 1;
528 m_chartypes[i].suffixes = &m_hashstrings[nextchar];
529 while (m_hashstrings[nextchar] != '\0')
530 nextchar += strlen (&m_hashstrings[nextchar]) + 1;
541 #define FREEP(p) do { if (p) free(p); } while (0)
545 * \param wchars Characters in -w option, if any
547 void ISpellChecker::initckch (char *wchars)
552 for (c = 0; c < static_cast<ichar_t>(SET_SIZE+ m_hashheader.nstrchars); ++c)
562 else if (isboundarych (c))
570 while (m_Trynum < SET_SIZE && *wchars != '\0')
572 if (*wchars != 'n' && *wchars != '\\')
584 if (isdigit (wchars[0]))
587 if (isdigit (wchars[1]))
590 if (isdigit (wchars[2]))
594 if (wchars[-1] == 'n')
596 wchars += strlen (num);
601 wchars += strlen (num);
618 if (!m_hashheader.wordchars[c])
620 m_hashheader.wordchars[c] = 1;
621 m_hashheader.sortorder[c] = m_hashheader.sortval++;
632 void ISpellChecker::clearindex (struct flagptr *indexp)
635 for (i = 0; i < SET_SIZE + m_hashheader.nstrchars; i++, indexp++)
637 if (indexp->numents == 0 && indexp->pu.fp != NULL)
639 clearindex(indexp->pu.fp);
646 static void dumpindex (indexp, depth)
647 register struct flagptr * indexp;
653 char stripbuf[INPUTWORDLEN + 4 * MAXAFFIXLEN + 4];
655 for (i = 0; i < SET_SIZE + hashheader.nstrchars; i++, indexp++)
657 if (indexp->numents == 0 && indexp->pu.fp != NULL)
659 for (j = depth; --j >= 0; )
661 if (i >= ' ' && i <= '~')
664 fprintf (stderr, "0x%x", i);
666 dumpindex (indexp->pu.fp, depth + 1);
668 else if (indexp->numents)
670 for (j = depth; --j >= 0; )
672 if (i >= ' ' && i <= '~')
675 fprintf (stderr, "0x%x", i);
676 fprintf (stderr, " -> %d entries\n", indexp->numents);
677 for (k = 0; k < indexp->numents; k++)
679 for (j = depth; --j >= 0; )
681 if (indexp->pu.ent[k].stripl)
683 ichartostr (stripbuf, indexp->pu.ent[k].strip,
685 fprintf (stderr, " entry %d (-%s,%s)\n",
686 &indexp->pu.ent[k] - sflaglist,
688 indexp->pu.ent[k].affl
689 ? ichartosstr (indexp->pu.ent[k].affix, 1) : "-");
692 fprintf (stderr, " entry %d (%s)\n",
693 &indexp->pu.ent[k] - sflaglist,
694 ichartosstr (indexp->pu.ent[k].affix, 1));
701 /* n is length of s */
709 struct dent * ISpellChecker::ispell_lookup (ichar_t *s, int dotree)
711 register struct dent * dp;
713 char schar[INPUTWORDLEN + MAXAFFIXLEN];
715 dp = &m_hashtbl[hash (s, m_hashsize)];
716 if (ichartostr (schar, s, sizeof schar, 1))
717 fprintf (stderr, WORD_TOO_LONG (schar));
718 for ( ; dp != NULL; dp = dp->next)
720 /* quick strcmp, but only for equality */
722 if (s1 && s1[0] == schar[0] && strcmp (s1 + 1, schar + 1) == 0)
724 #ifndef NO_CAPITALIZATION_SUPPORT
725 while (dp->flagfield & MOREVARIANTS) /* Skip variations */
732 void ISpellChecker::alloc_ispell_struct()
735 m_translate_out = G_ICONV_INVALID;
738 void ISpellChecker::free_ispell_struct()
740 if (g_iconv_is_valid(m_translate_in))
741 g_iconv_close (m_translate_in);
742 if (g_iconv_is_valid(m_translate_out))
743 g_iconv_close (m_translate_out);