-
Notifications
You must be signed in to change notification settings - Fork 55
/
lib.c
1489 lines (1219 loc) · 40 KB
/
lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* enchant
* Copyright (C) 2003, 2004 Dom Lachowicz
* Copyright (C) 2017 Reuben Thomas <rrt@sc3d.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders
* give permission to link the code of this program with
* non-LGPL Spelling Provider libraries (eg: a MSFT Office
* spell checker backend) and distribute linked combinations including
* the two. You must obey the GNU Lesser General Public License in all
* respects for all of the code used other than said providers. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <gmodule.h>
#include <glib/gstdio.h>
#include <locale.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "enchant.h"
#include "enchant-provider.h"
#include "pwl.h"
#include "unused-parameter.h"
#include "relocatable.h"
#include "configmake.h"
/********************************************************************************/
struct str_enchant_broker
{
GSList *provider_list; /* list of all of the spelling backend providers */
GHashTable *dict_map; /* map of language tag -> dictionary */
GHashTable *provider_ordering; /* map of language tag -> provider order */
gchar * error;
};
typedef struct str_enchant_session
{
GHashTable *session_include;
GHashTable *session_exclude;
EnchantPWL *personal;
EnchantPWL *exclude;
char * personal_filename;
char * exclude_filename;
char * language_tag;
char * error;
gboolean is_pwl;
EnchantProvider * provider;
} EnchantSession;
typedef struct str_enchant_dict_private_data
{
unsigned int reference_count;
EnchantSession* session;
} EnchantDictPrivateData;
typedef EnchantProvider *(*EnchantProviderInitFunc) (void);
typedef void (*EnchantPreConfigureFunc) (EnchantProvider * provider, const char * module_dir);
/********************************************************************************/
/********************************************************************************/
/* Relocate a path and ensure the result is allocated on the heap */
char *
enchant_relocate (const char *path)
{
char *newpath = (char *) relocate (path);
if (path == newpath)
newpath = strdup (newpath);
return newpath;
}
static void
enchant_ensure_dir_exists (const char* dir)
{
if (dir && !g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
{
(void)g_remove (dir);
g_mkdir_with_parents (dir, 0700);
}
}
char *
enchant_get_user_config_dir (void)
{
const gchar * env = g_getenv("ENCHANT_CONFIG_DIR");
if (env)
return g_filename_to_utf8(env, -1, NULL, NULL, NULL);
return g_build_filename (g_get_user_config_dir (), "enchant", NULL);
}
GSList *
enchant_get_conf_dirs (void)
{
GSList *conf_dirs = NULL;
char *pkgdatadir = NULL;
char *sysconfdir = NULL;
char *pkgconfdir = NULL;
char *user_config_dir = NULL;
if ((pkgdatadir = enchant_relocate (PKGDATADIR)) == NULL)
goto error_exit;
conf_dirs = g_slist_append (conf_dirs, pkgdatadir);
if ((sysconfdir = enchant_relocate (SYSCONFDIR)) == NULL)
goto error_exit;
if ((pkgconfdir = g_build_filename (sysconfdir, "enchant", NULL)) == NULL)
goto error_exit;
conf_dirs = g_slist_append (conf_dirs, pkgconfdir);
free (sysconfdir);
if ((user_config_dir = enchant_get_user_config_dir ()) == NULL)
goto error_exit;
conf_dirs = g_slist_append (conf_dirs, user_config_dir);
return conf_dirs;
error_exit:
free (pkgdatadir);
free (sysconfdir);
g_free (pkgconfdir);
free (user_config_dir);
return NULL;
}
/********************************************************************************/
/********************************************************************************/
/* returns TRUE if tag is valid
* for requires alphanumeric ASCII or underscore
*/
static _GL_ATTRIBUTE_PURE int
enchant_is_valid_dictionary_tag(const char * const tag)
{
const char * it;
for (it = tag; *it; ++it)
{
if(!g_ascii_isalnum(*it) && *it != '_')
return 0;
}
return it != tag; /*empty tag invalid*/
}
static char *
enchant_normalize_dictionary_tag (const char * const dict_tag)
{
char * new_tag = g_strstrip (strdup (dict_tag));
/* strip off en_GB@euro */
*strchrnul (new_tag, '@') = '\0';
/* strip off en_GB.UTF-8 */
*strchrnul (new_tag, '.') = '\0';
/* turn en-GB into en_GB */
char * needle;
if ((needle = strchr (new_tag, '-')) != NULL)
*needle = '_';
/* everything before first '_' is converted to lower case */
needle = strchrnul (new_tag, '_');
for (gchar *it = new_tag; it != needle; ++it)
*it = g_ascii_tolower (*it);
/* everything after first '_' is converted to upper case */
for (gchar *it = needle; *it; ++it)
*it = g_ascii_toupper (*it);
return new_tag;
}
static char *
enchant_iso_639_from_tag (const char * const dict_tag)
{
char * new_tag = strdup (dict_tag);
char * needle = strchr (new_tag, '_');
if (needle != NULL)
*needle = '\0';
return new_tag;
}
static void
enchant_session_destroy (EnchantSession * session)
{
g_hash_table_destroy (session->session_include);
g_hash_table_destroy (session->session_exclude);
enchant_pwl_free (session->personal);
enchant_pwl_free (session->exclude);
g_free (session->personal_filename);
g_free (session->exclude_filename);
free (session->language_tag);
if (session->error)
g_free (session->error);
g_free (session);
}
static EnchantSession *
enchant_session_new_with_pwl (EnchantProvider * provider,
const char * const pwl,
const char * const excl,
const char * const lang,
gboolean fail_if_no_pwl)
{
EnchantPWL *personal = NULL;
if (pwl)
personal = enchant_pwl_init_with_file (pwl);
if (personal == NULL) {
if (fail_if_no_pwl)
return NULL;
else
personal = enchant_pwl_init ();
}
EnchantPWL *exclude = NULL;
if (excl)
exclude = enchant_pwl_init_with_file (excl);
if (exclude == NULL)
exclude = enchant_pwl_init ();
EnchantSession * session = g_new0 (EnchantSession, 1);
session->session_include = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
session->session_exclude = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
session->personal = personal;
session->exclude = exclude;
session->provider = provider;
session->language_tag = strdup (lang);
session->personal_filename = g_strdup (pwl); /* Need g_strdup because may be NULL */
session->exclude_filename = g_strdup (excl); /* Need g_strdup because may be NULL */
return session;
}
static EnchantSession *
_enchant_session_new (EnchantProvider *provider, const char * const user_config_dir,
const char * const lang, gboolean fail_if_no_pwl)
{
if (!user_config_dir || !lang)
return NULL;
char *filename = g_strdup_printf ("%s.dic", lang);
char *dic = g_build_filename (user_config_dir, filename, NULL);
g_free (filename);
filename = g_strdup_printf ("%s.exc", lang);
char *excl = g_build_filename (user_config_dir, filename, NULL);
g_free (filename);
EnchantSession * session = enchant_session_new_with_pwl (provider, dic, excl, lang, fail_if_no_pwl);
g_free (dic);
g_free (excl);
return session;
}
static EnchantSession *
enchant_session_new (EnchantProvider *provider, const char * const lang)
{
char *user_config_dir = enchant_get_user_config_dir ();
EnchantSession * session = NULL;
session = _enchant_session_new (provider, user_config_dir, lang, TRUE);
if (session == NULL && user_config_dir != NULL)
{
enchant_ensure_dir_exists (user_config_dir);
session = _enchant_session_new (provider, user_config_dir, lang, FALSE);
}
g_free (user_config_dir);
return session;
}
static void
enchant_session_add (EnchantSession * session, const char * const word, size_t len)
{
char* key = g_strndup (word, len);
g_hash_table_remove (session->session_exclude, key);
g_hash_table_insert (session->session_include, key, GINT_TO_POINTER(TRUE));
}
static void
enchant_session_remove (EnchantSession * session, const char * const word, size_t len)
{
char* key = g_strndup (word, len);
g_hash_table_remove (session->session_include, key);
g_hash_table_insert (session->session_exclude, key, GINT_TO_POINTER(TRUE));
}
static void
enchant_session_add_personal (EnchantSession * session, const char * const word, size_t len)
{
enchant_pwl_add(session->personal, word, len);
}
static void
enchant_session_remove_personal (EnchantSession * session, const char * const word, size_t len)
{
enchant_pwl_remove(session->personal, word, len);
}
static void
enchant_session_add_exclude (EnchantSession * session, const char * const word, size_t len)
{
enchant_pwl_add(session->exclude, word, len);
}
static void
enchant_session_remove_exclude (EnchantSession * session, const char * const word, size_t len)
{
enchant_pwl_remove(session->exclude, word, len);
}
/* a word is excluded if it is in the exclude dictionary or in the session exclude list
* AND the word has not been added to the session include list
*/
static gboolean
enchant_session_exclude (EnchantSession * session, const char * const word, size_t len)
{
char * utf = g_strndup (word, len);
gboolean result = !g_hash_table_lookup (session->session_include, utf) &&
(g_hash_table_lookup (session->session_exclude, utf) ||
enchant_pwl_check (session->exclude, word, len) == 0);
g_free (utf);
return result;
}
static gboolean
enchant_session_contains (EnchantSession * session, const char * const word, size_t len)
{
char * utf = g_strndup (word, len);
gboolean result = g_hash_table_lookup (session->session_include, utf) ||
(enchant_pwl_check (session->personal, word, len) == 0 &&
(!enchant_pwl_check (session->exclude, word, len)) == 0);
g_free (utf);
return result;
}
static void
enchant_session_clear_error (EnchantSession * session)
{
if (session->error)
{
g_free (session->error);
session->error = NULL;
}
}
/********************************************************************************/
/********************************************************************************/
static void
enchant_free_string_list (char ** string_list)
{
g_strfreev (string_list);
}
void
enchant_dict_set_error (EnchantDict * dict, const char * const err)
{
g_return_if_fail (dict);
g_return_if_fail (err);
g_return_if_fail (g_utf8_validate(err, -1, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
session->error = strdup (err);
}
const char *
enchant_dict_get_error (EnchantDict * dict)
{
g_return_val_if_fail (dict, NULL);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
return session->error;
}
int
enchant_dict_check (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_val_if_fail (dict, -1);
g_return_val_if_fail (word, -1);
if (len < 0)
len = strlen (word);
g_return_val_if_fail (len, -1);
g_return_val_if_fail (g_utf8_validate(word, len, NULL),-1);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
/* first, see if it's to be excluded*/
if (enchant_session_exclude (session, word, len))
return 1;
/* then, see if it's in our pwl or session*/
if (enchant_session_contains(session, word, len))
return 0;
if (dict->check)
return (*dict->check) (dict, word, len);
else if (session->is_pwl)
return 1;
return -1;
}
/* @suggs must have at least n_suggs + n_new_suggs space allocated
* @n_suggs is the number if items currently appearing in @suggs
*
* returns the number of items in @suggs after merge is complete
*/
static int
enchant_dict_merge_suggestions(char ** suggs, size_t n_suggs, char ** new_suggs, size_t n_new_suggs)
{
for (size_t i = 0; i < n_new_suggs; i++)
{
char * normalized_new_sugg = g_utf8_normalize (new_suggs[i], -1, G_NORMALIZE_NFD);
int is_duplicate = 0;
for (size_t j = 0; !is_duplicate && j < n_suggs; j++)
{
char* normalized_sugg = g_utf8_normalize (suggs[j], -1, G_NORMALIZE_NFD);
is_duplicate = strcmp (normalized_sugg, normalized_new_sugg) == 0;
g_free (normalized_sugg);
}
g_free (normalized_new_sugg);
if (!is_duplicate)
suggs[n_suggs++] = strdup (new_suggs[i]);
}
return n_suggs;
}
static char **
enchant_dict_get_good_suggestions(EnchantDict * dict, char ** suggs, size_t n_suggs, size_t* out_n_filtered_suggs)
{
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
char ** filtered_suggs = g_new0 (char *, n_suggs + 1);
size_t n_filtered_suggs = 0;
for (size_t i = 0; i < n_suggs; i++)
{
size_t sugg_len = strlen(suggs[i]);
if (sugg_len == 0)
continue;
if (g_utf8_validate(suggs[i], sugg_len, NULL) &&
!enchant_session_exclude(session, suggs[i], sugg_len))
filtered_suggs[n_filtered_suggs++] = strdup (suggs[i]);
}
if (out_n_filtered_suggs)
*out_n_filtered_suggs = n_filtered_suggs;
return filtered_suggs;
}
char **
enchant_dict_suggest (EnchantDict * dict, const char *const word, ssize_t len, size_t * out_n_suggs)
{
g_return_val_if_fail (dict, NULL);
g_return_val_if_fail (word, NULL);
if (len < 0)
len = strlen (word);
g_return_val_if_fail (len, NULL);
g_return_val_if_fail (g_utf8_validate(word, len, NULL), NULL);
size_t n_dict_suggs = 0, n_pwl_suggs = 0, n_suggsT = 0;
char **dict_suggs = NULL, **pwl_suggs = NULL, **suggsT;
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
/* Check for suggestions from provider dictionary */
if (dict->suggest)
{
dict_suggs = (*dict->suggest) (dict, word, len, &n_dict_suggs);
if (dict_suggs)
{
suggsT = enchant_dict_get_good_suggestions(dict, dict_suggs, n_dict_suggs, &n_suggsT);
enchant_free_string_list (dict_suggs);
dict_suggs = suggsT;
n_dict_suggs = n_suggsT;
}
}
/* Check for suggestions from personal dictionary */
if (session->personal)
{
pwl_suggs = enchant_pwl_suggest(session->personal, word, len, dict_suggs, &n_pwl_suggs);
if (pwl_suggs)
{
suggsT = enchant_dict_get_good_suggestions(dict, pwl_suggs, n_pwl_suggs, &n_suggsT);
enchant_free_string_list (pwl_suggs);
pwl_suggs = suggsT;
n_pwl_suggs = n_suggsT;
}
}
/* Clone suggestions, if any */
char **suggs = NULL;
size_t n_suggs = n_pwl_suggs + n_dict_suggs;
if (n_suggs > 0)
{
suggs = g_new0 (char *, n_suggs + 1);
n_suggs = enchant_dict_merge_suggestions(suggs, 0, dict_suggs, n_dict_suggs);
n_suggs = enchant_dict_merge_suggestions(suggs, n_suggs, pwl_suggs, n_pwl_suggs);
}
g_strfreev(dict_suggs);
g_strfreev(pwl_suggs);
if (out_n_suggs)
*out_n_suggs = n_suggs;
return suggs;
}
void
enchant_dict_add (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_if_fail (dict);
g_return_if_fail (word);
if (len < 0)
len = strlen (word);
g_return_if_fail (len);
g_return_if_fail (g_utf8_validate(word, len, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
enchant_session_add_personal (session, word, len);
enchant_session_remove_exclude (session, word, len);
if (dict->add_to_personal)
(*dict->add_to_personal) (dict, word, len);
}
void
enchant_dict_add_to_session (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_if_fail (dict);
g_return_if_fail (word);
if (len < 0)
len = strlen (word);
g_return_if_fail (len);
g_return_if_fail (g_utf8_validate(word, len, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
enchant_session_add (session, word, len);
if (dict->add_to_session)
(*dict->add_to_session) (dict, word, len);
}
int
enchant_dict_is_added (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_val_if_fail (dict, 0);
g_return_val_if_fail (word, 0);
if (len < 0)
len = strlen (word);
g_return_val_if_fail (len, 0);
g_return_val_if_fail (g_utf8_validate(word, len, NULL), 0);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
return enchant_session_contains (session, word, len);
}
void
enchant_dict_remove (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_if_fail (dict);
g_return_if_fail (word);
if (len < 0)
len = strlen (word);
g_return_if_fail (len);
g_return_if_fail (g_utf8_validate(word, len, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
enchant_session_remove_personal (session, word, len);
enchant_session_add_exclude(session, word, len);
if (dict->add_to_exclude)
(*dict->add_to_exclude) (dict, word, len);
}
void
enchant_dict_remove_from_session (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_if_fail (dict);
g_return_if_fail (word);
if (len < 0)
len = strlen (word);
g_return_if_fail (len);
g_return_if_fail (g_utf8_validate(word, len, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
enchant_session_remove (session, word, len);
}
int
enchant_dict_is_removed (EnchantDict * dict, const char *const word, ssize_t len)
{
g_return_val_if_fail (dict, 0);
g_return_val_if_fail (word, 0);
if (len < 0)
len = strlen (word);
g_return_val_if_fail (len, 0);
g_return_val_if_fail (g_utf8_validate(word, len, NULL), 0);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
return enchant_session_exclude (session, word, len);
}
void
enchant_dict_store_replacement (EnchantDict * dict,
const char *const mis, ssize_t mis_len,
const char *const cor, ssize_t cor_len)
{
g_return_if_fail (dict);
g_return_if_fail (mis);
g_return_if_fail (cor);
if (mis_len < 0)
mis_len = strlen (mis);
if (cor_len < 0)
cor_len = strlen (cor);
g_return_if_fail (mis_len);
g_return_if_fail (cor_len);
g_return_if_fail (g_utf8_validate(mis, mis_len, NULL));
g_return_if_fail (g_utf8_validate(cor, cor_len, NULL));
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
/* if it's not implemented, it's not worth emulating */
if (dict->store_replacement)
(*dict->store_replacement) (dict, mis, mis_len, cor, cor_len);
}
void
enchant_dict_free_string_list (EnchantDict * dict, char **string_list)
{
g_return_if_fail (dict);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
g_strfreev(string_list);
}
void
enchant_dict_describe (EnchantDict * dict, EnchantDictDescribeFn fn, void * user_data)
{
g_return_if_fail (dict);
g_return_if_fail (fn);
EnchantSession * session = ((EnchantDictPrivateData*)dict->enchant_private_data)->session;
enchant_session_clear_error (session);
EnchantProvider * provider = session->provider;
const char * name, * desc, * file;
if (provider)
{
GModule *module = (GModule *) provider->enchant_private_data;
file = g_module_name (module);
name = (*provider->identify) (provider);
desc = (*provider->describe) (provider);
}
else
{
file = session->personal_filename;
name = "Personal Wordlist";
desc = "Personal Wordlist";
}
const char *tag = session->language_tag;
(*fn) (tag, name, desc, file, user_data);
}
/***********************************************************************************/
/***********************************************************************************/
static void
enchant_broker_clear_error (EnchantBroker * broker)
{
if (broker->error)
{
g_free (broker->error);
broker->error = NULL;
}
}
static void
enchant_broker_set_error (EnchantBroker * broker, const char * const err)
{
enchant_broker_clear_error (broker);
broker->error = strdup (err);
}
static int
enchant_provider_is_valid(EnchantProvider * provider)
{
if (provider == NULL)
g_warning ("EnchantProvider cannot be NULL\n");
else if (provider->identify == NULL)
g_warning ("EnchantProvider's identify method cannot be NULL\n");
else if (!g_utf8_validate((*provider->identify)(provider), -1, NULL))
g_warning ("EnchantProvider's identify method does not return valid UTF-8.\n");
else if (provider->describe == NULL)
g_warning ("EnchantProvider's describe method cannot be NULL\n");
else if (!g_utf8_validate((*provider->describe)(provider), -1, NULL))
g_warning ("EnchantProvider's describe method does not return valid UTF-8.\n");
else if (provider->dispose == NULL)
g_warning ("EnchantProvider's dispose method cannot be NULL\n");
else if (provider->dispose_dict == NULL)
g_warning ("EnchantProvider's dispose_dict method cannot be NULL\n");
else if (provider->list_dicts == NULL)
g_warning ("EnchantProvider's list_dicts method cannot be NULL\n");
else
return 1;
return 0;
}
static void
enchant_load_providers_in_dir (EnchantBroker * broker, const char *dir_name)
{
GDir *dir = g_dir_open (dir_name, 0, NULL);
if (!dir)
return;
size_t g_module_suffix_len = strlen (G_MODULE_SUFFIX);
const char *dir_entry;
while ((dir_entry = g_dir_read_name (dir)) != NULL)
{
GModule *module = NULL;
EnchantProvider *provider = NULL;
size_t entry_len = strlen (dir_entry);
if ((entry_len > g_module_suffix_len) &&
dir_entry[0] != '.' && /* Skip hidden files */
!strcmp(dir_entry+(entry_len-g_module_suffix_len), G_MODULE_SUFFIX))
{
#ifdef _WIN32
/* Suppress error popups for failing to load plugins */
UINT old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
char * filename = g_build_filename (dir_name, dir_entry, NULL);
module = g_module_open (filename, (GModuleFlags) 0);
if (module)
{
EnchantProviderInitFunc init_func;
if (g_module_symbol (module, "init_enchant_provider", (gpointer *) (&init_func))
&& init_func)
{
provider = init_func ();
if (!enchant_provider_is_valid(provider))
{
g_warning ("Error loading plugin: %s's init_enchant_provider returned invalid provider.\n", dir_entry);
if(provider)
{
provider->dispose(provider);
provider = NULL;
}
g_module_close (module);
}
}
else
{
g_module_close (module);
}
}
else
{
g_warning ("Error loading plugin: %s\n", g_module_error());
}
g_free (filename);
#ifdef _WIN32
/* Restore the original error mode */
SetErrorMode(old_error_mode);
#endif
}
if (provider)
{
/* optional entry point to allow modules to look for associated files */
EnchantPreConfigureFunc conf_func;
if (g_module_symbol (module, "configure_enchant_provider", (gpointer *) (&conf_func))
&& conf_func)
{
conf_func (provider, dir_name);
if (!enchant_provider_is_valid(provider))
{
g_warning ("Error loading plugin: %s's configure_enchant_provider modified provider and it is now invalid.\n", dir_entry);
provider->dispose(provider);
provider = NULL;
g_module_close (module);
}
}
}
if (provider)
{
provider->enchant_private_data = (void *) module;
provider->owner = broker;
broker->provider_list = g_slist_append (broker->provider_list, (gpointer)provider);
}
}
g_dir_close (dir);
}
static void
enchant_load_providers (EnchantBroker * broker)
{
char *module_dir = enchant_relocate (PKGLIBDIR "-" ENCHANT_MAJOR_VERSION);
if (module_dir)
enchant_load_providers_in_dir (broker, module_dir);
free (module_dir);
}
static void
enchant_load_ordering_from_file (EnchantBroker * broker, const char * file)
{
GIOChannel * ch = g_io_channel_new_file (file, "r", NULL);
if (!ch)
return;
gchar *line;
gsize terminator;
while (G_IO_STATUS_NORMAL == g_io_channel_read_line (ch, &line, NULL, &terminator, NULL)) {
char *colon = strchr (line, ':');
if (colon != NULL)
{
char * tag = g_strndup (line, colon - line);
char * ordering = g_strndup (colon + 1, terminator - 1);
enchant_broker_set_ordering (broker, tag, ordering);
g_free (tag);
g_free (ordering);
}
g_free (line);
}
g_io_channel_unref (ch);
}
static void
enchant_load_provider_ordering (EnchantBroker * broker)
{
broker->provider_ordering = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
GSList *conf_dirs = enchant_get_conf_dirs ();
for (GSList *iter = conf_dirs; iter; iter = iter->next)
{
char *ordering_file = g_build_filename (iter->data, "enchant.ordering", NULL);
enchant_load_ordering_from_file (broker, ordering_file);
g_free (ordering_file);
}
g_slist_free_full (conf_dirs, g_free);
}
static GSList *
enchant_get_ordered_providers (EnchantBroker * broker, const char * const tag)
{
char * ordering = (char *)g_hash_table_lookup (broker->provider_ordering, (gpointer)tag);
if (!ordering)
ordering = (char *)g_hash_table_lookup (broker->provider_ordering, (gpointer)"*");
GSList * list = NULL;
if (ordering)
{
char **tokens = g_strsplit (ordering, ",", 0);
if (tokens)
{
for (size_t i = 0; tokens[i]; i++)
{
char *token = g_strstrip(tokens[i]);
for (GSList * iter = broker->provider_list; iter != NULL; iter = g_slist_next (iter))
{
EnchantProvider *provider = (EnchantProvider*)iter->data;
if (provider && !strcmp (token, (*provider->identify)(provider)))
list = g_slist_append (list, (gpointer)provider);
}
}
g_strfreev (tokens);
}
}
/* append providers not in the list, or from an unordered list */
for (GSList * iter = broker->provider_list; iter != NULL; iter = g_slist_next (iter))
{
if (!g_slist_find (list, iter->data))
list = g_slist_append (list, iter->data);
}
return list;
}
static void
enchant_dict_destroyed (gpointer data)
{
g_return_if_fail (data);
EnchantDict *dict = (EnchantDict *) data;
EnchantDictPrivateData *enchant_dict_private_data = (EnchantDictPrivateData*)dict->enchant_private_data;
EnchantSession *session = enchant_dict_private_data->session;
EnchantProvider *owner = session->provider;
if (owner)
(*owner->dispose_dict) (owner, dict);
else if(session->is_pwl)
g_free (dict);
g_free(enchant_dict_private_data);
enchant_session_destroy (session);
}
static void
enchant_provider_free (gpointer data)
{
g_return_if_fail (data);
EnchantProvider *provider = (EnchantProvider *) data;
GModule *module = (GModule *) provider->enchant_private_data;
(*provider->dispose) (provider);