-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspellfile.c
6712 lines (6076 loc) · 179 KB
/
spellfile.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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* spellfile.c: code for reading and writing spell files.
*
* See spell.c for information about spell checking.
*/
/*
* Vim spell file format: <HEADER>
* <SECTIONS>
* <LWORDTREE>
* <KWORDTREE>
* <PREFIXTREE>
*
* <HEADER>: <fileID> <versionnr>
*
* <fileID> 8 bytes "VIMspell"
* <versionnr> 1 byte VIMSPELLVERSION
*
*
* Sections make it possible to add information to the .spl file without
* making it incompatible with previous versions. There are two kinds of
* sections:
* 1. Not essential for correct spell checking. E.g. for making suggestions.
* These are skipped when not supported.
* 2. Optional information, but essential for spell checking when present.
* E.g. conditions for affixes. When this section is present but not
* supported an error message is given.
*
* <SECTIONS>: <section> ... <sectionend>
*
* <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
*
* <sectionID> 1 byte number from 0 to 254 identifying the section
*
* <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
* spell checking
*
* <sectionlen> 4 bytes length of section contents, MSB first
*
* <sectionend> 1 byte SN_END
*
*
* sectionID == SN_INFO: <infotext>
* <infotext> N bytes free format text with spell file info (version,
* website, etc)
*
* sectionID == SN_REGION: <regionname> ...
* <regionname> 2 bytes Up to MAXREGIONS region names: ca, au, etc. Lower
* case. First <regionname> is region 1.
*
* sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
* <folcharslen> <folchars>
* <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
* <charflags> N bytes List of flags (first one is for character 128):
* 0x01 word character CF_WORD
* 0x02 upper-case character CF_UPPER
* <folcharslen> 2 bytes Number of bytes in <folchars>.
* <folchars> N bytes Folded characters, first one is for character 128.
*
* sectionID == SN_MIDWORD: <midword>
* <midword> N bytes Characters that are word characters only when used
* in the middle of a word.
*
* sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
* <prefcondcnt> 2 bytes Number of <prefcond> items following.
* <prefcond> : <condlen> <condstr>
* <condlen> 1 byte Length of <condstr>.
* <condstr> N bytes Condition for the prefix.
*
* sectionID == SN_REP: <repcount> <rep> ...
* <repcount> 2 bytes number of <rep> items, MSB first.
* <rep> : <repfromlen> <repfrom> <reptolen> <repto>
* <repfromlen> 1 byte length of <repfrom>
* <repfrom> N bytes "from" part of replacement
* <reptolen> 1 byte length of <repto>
* <repto> N bytes "to" part of replacement
*
* sectionID == SN_REPSAL: <repcount> <rep> ...
* just like SN_REP but for soundfolded words
*
* sectionID == SN_SAL: <salflags> <salcount> <sal> ...
* <salflags> 1 byte flags for soundsalike conversion:
* SAL_F0LLOWUP
* SAL_COLLAPSE
* SAL_REM_ACCENTS
* <salcount> 2 bytes number of <sal> items following
* <sal> : <salfromlen> <salfrom> <saltolen> <salto>
* <salfromlen> 1 byte length of <salfrom>
* <salfrom> N bytes "from" part of soundsalike
* <saltolen> 1 byte length of <salto>
* <salto> N bytes "to" part of soundsalike
*
* sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
* <sofofromlen> 2 bytes length of <sofofrom>
* <sofofrom> N bytes "from" part of soundfold
* <sofotolen> 2 bytes length of <sofoto>
* <sofoto> N bytes "to" part of soundfold
*
* sectionID == SN_SUGFILE: <timestamp>
* <timestamp> 8 bytes time in seconds that must match with .sug file
*
* sectionID == SN_NOSPLITSUGS: nothing
*
* sectionID == SN_NOCOMPOUNDSUGS: nothing
*
* sectionID == SN_WORDS: <word> ...
* <word> N bytes NUL terminated common word
*
* sectionID == SN_MAP: <mapstr>
* <mapstr> N bytes String with sequences of similar characters,
* separated by slashes.
*
* sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
* <comppatcount> <comppattern> ... <compflags>
* <compmax> 1 byte Maximum nr of words in compound word.
* <compminlen> 1 byte Minimal word length for compounding.
* <compsylmax> 1 byte Maximum nr of syllables in compound word.
* <compoptions> 2 bytes COMP_ flags.
* <comppatcount> 2 bytes number of <comppattern> following
* <compflags> N bytes Flags from COMPOUNDRULE items, separated by
* slashes.
*
* <comppattern>: <comppatlen> <comppattext>
* <comppatlen> 1 byte length of <comppattext>
* <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
*
* sectionID == SN_NOBREAK: (empty, its presence is what matters)
*
* sectionID == SN_SYLLABLE: <syllable>
* <syllable> N bytes String from SYLLABLE item.
*
* <LWORDTREE>: <wordtree>
*
* <KWORDTREE>: <wordtree>
*
* <PREFIXTREE>: <wordtree>
*
*
* <wordtree>: <nodecount> <nodedata> ...
*
* <nodecount> 4 bytes Number of nodes following. MSB first.
*
* <nodedata>: <siblingcount> <sibling> ...
*
* <siblingcount> 1 byte Number of siblings in this node. The siblings
* follow in sorted order.
*
* <sibling>: <byte> [ <nodeidx> <xbyte>
* | <flags> [<flags2>] [<region>] [<affixID>]
* | [<pflags>] <affixID> <prefcondnr> ]
*
* <byte> 1 byte Byte value of the sibling. Special cases:
* BY_NOFLAGS: End of word without flags and for all
* regions.
* For PREFIXTREE <affixID> and
* <prefcondnr> follow.
* BY_FLAGS: End of word, <flags> follow.
* For PREFIXTREE <pflags>, <affixID>
* and <prefcondnr> follow.
* BY_FLAGS2: End of word, <flags> and <flags2>
* follow. Not used in PREFIXTREE.
* BY_INDEX: Child of sibling is shared, <nodeidx>
* and <xbyte> follow.
*
* <nodeidx> 3 bytes Index of child for this sibling, MSB first.
*
* <xbyte> 1 byte byte value of the sibling.
*
* <flags> 1 byte bitmask of:
* WF_ALLCAP word must have only capitals
* WF_ONECAP first char of word must be capital
* WF_KEEPCAP keep-case word
* WF_FIXCAP keep-case word, all caps not allowed
* WF_RARE rare word
* WF_BANNED bad word
* WF_REGION <region> follows
* WF_AFX <affixID> follows
*
* <flags2> 1 byte Bitmask of:
* WF_HAS_AFF >> 8 word includes affix
* WF_NEEDCOMP >> 8 word only valid in compound
* WF_NOSUGGEST >> 8 word not used for suggestions
* WF_COMPROOT >> 8 word already a compound
* WF_NOCOMPBEF >> 8 no compounding before this word
* WF_NOCOMPAFT >> 8 no compounding after this word
*
* <pflags> 1 byte bitmask of:
* WFP_RARE rare prefix
* WFP_NC non-combining prefix
* WFP_UP letter after prefix made upper case
*
* <region> 1 byte Bitmask for regions in which word is valid. When
* omitted it's valid in all regions.
* Lowest bit is for region 1.
*
* <affixID> 1 byte ID of affix that can be used with this word. In
* PREFIXTREE used for the required prefix ID.
*
* <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
* from HEADER.
*
* All text characters are in 'encoding', but stored as single bytes.
*/
/*
* Vim .sug file format: <SUGHEADER>
* <SUGWORDTREE>
* <SUGTABLE>
*
* <SUGHEADER>: <fileID> <versionnr> <timestamp>
*
* <fileID> 6 bytes "VIMsug"
* <versionnr> 1 byte VIMSUGVERSION
* <timestamp> 8 bytes timestamp that must match with .spl file
*
*
* <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
*
*
* <SUGTABLE>: <sugwcount> <sugline> ...
*
* <sugwcount> 4 bytes number of <sugline> following
*
* <sugline>: <sugnr> ... NUL
*
* <sugnr>: X bytes word number that results in this soundfolded word,
* stored as an offset to the previous number in as
* few bytes as possible, see offset2bytes())
*/
#include "vim.h"
#if defined(FEAT_SPELL) || defined(PROTO)
#ifndef UNIX // it's in os_unix.h for Unix
# include <time.h> // for time_t
#endif
#ifndef UNIX // it's in os_unix.h for Unix
# include <time.h> // for time_t
#endif
// Special byte values for <byte>. Some are only used in the tree for
// postponed prefixes, some only in the other trees. This is a bit messy...
#define BY_NOFLAGS 0 // end of word without flags or region; for
// postponed prefix: no <pflags>
#define BY_INDEX 1 // child is shared, index follows
#define BY_FLAGS 2 // end of word, <flags> byte follows; for
// postponed prefix: <pflags> follows
#define BY_FLAGS2 3 // end of word, <flags> and <flags2> bytes
// follow; never used in prefix tree
#define BY_SPECIAL BY_FLAGS2 // highest special byte value
#define ZERO_FLAG 65009 // used when flag is zero: "0"
// Flags used in .spl file for soundsalike flags.
#define SAL_F0LLOWUP 1
#define SAL_COLLAPSE 2
#define SAL_REM_ACCENTS 4
#define VIMSPELLMAGIC "VIMspell" // string at start of Vim spell file
#define VIMSPELLMAGICL 8
#define VIMSPELLVERSION 50
// Section IDs. Only renumber them when VIMSPELLVERSION changes!
#define SN_REGION 0 // <regionname> section
#define SN_CHARFLAGS 1 // charflags section
#define SN_MIDWORD 2 // <midword> section
#define SN_PREFCOND 3 // <prefcond> section
#define SN_REP 4 // REP items section
#define SN_SAL 5 // SAL items section
#define SN_SOFO 6 // soundfolding section
#define SN_MAP 7 // MAP items section
#define SN_COMPOUND 8 // compound words section
#define SN_SYLLABLE 9 // syllable section
#define SN_NOBREAK 10 // NOBREAK section
#define SN_SUGFILE 11 // timestamp for .sug file
#define SN_REPSAL 12 // REPSAL items section
#define SN_WORDS 13 // common words
#define SN_NOSPLITSUGS 14 // don't split word for suggestions
#define SN_INFO 15 // info section
#define SN_NOCOMPOUNDSUGS 16 // don't compound for suggestions
#define SN_END 255 // end of sections
#define SNF_REQUIRED 1 // <sectionflags>: required section
#define CF_WORD 0x01
#define CF_UPPER 0x02
/*
* Loop through all the siblings of a node (including the node)
*/
#define FOR_ALL_NODE_SIBLINGS(node, np) \
for ((np) = (node); (np) != NULL; (np) = (np)->wn_sibling)
static int set_spell_finish(spelltab_T *new_st);
static int write_spell_prefcond(FILE *fd, garray_T *gap, size_t *fwv);
static int read_region_section(FILE *fd, slang_T *slang, int len);
static int read_charflags_section(FILE *fd);
static int read_prefcond_section(FILE *fd, slang_T *lp);
static int read_rep_section(FILE *fd, garray_T *gap, short *first);
static int read_sal_section(FILE *fd, slang_T *slang);
static int read_words_section(FILE *fd, slang_T *lp, int len);
static int read_sofo_section(FILE *fd, slang_T *slang);
static int read_compound(FILE *fd, slang_T *slang, int len);
static int set_sofo(slang_T *lp, char_u *from, char_u *to);
static void set_sal_first(slang_T *lp);
static int *mb_str2wide(char_u *s);
static int spell_read_tree(FILE *fd, char_u **bytsp, long *bytsp_len, idx_T **idxsp, int prefixtree, int prefixcnt);
static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
static void set_map_str(slang_T *lp, char_u *map);
static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
static char *e_affname = N_("Affix name too long in %s line %d: %s");
static char *msg_compressing = N_("Compressing word tree...");
/*
* Load one spell file and store the info into a slang_T.
*
* This is invoked in three ways:
* - From spell_load_cb() to load a spell file for the first time. "lang" is
* the language name, "old_lp" is NULL. Will allocate an slang_T.
* - To reload a spell file that was changed. "lang" is NULL and "old_lp"
* points to the existing slang_T.
* - Just after writing a .spl file; it's read back to produce the .sug file.
* "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
*
* Returns the slang_T the spell file was loaded into. NULL for error.
*/
slang_T *
spell_load_file(
char_u *fname,
char_u *lang,
slang_T *old_lp,
int silent) // no error if file doesn't exist
{
FILE *fd;
char_u buf[VIMSPELLMAGICL];
char_u *p;
int i;
int n;
int len;
slang_T *lp = NULL;
int c = 0;
int res;
int did_estack_push = FALSE;
ESTACK_CHECK_DECLARATION
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
if (!silent)
semsg(_(e_cant_open_file_str), fname);
else if (p_verbose > 2)
{
verbose_enter();
smsg((const char *)e_cant_open_file_str, fname);
verbose_leave();
}
goto endFAIL;
}
if (p_verbose > 2)
{
verbose_enter();
smsg(_("Reading spell file \"%s\""), fname);
verbose_leave();
}
if (old_lp == NULL)
{
lp = slang_alloc(lang);
if (lp == NULL)
goto endFAIL;
// Remember the file name, used to reload the file when it's updated.
lp->sl_fname = vim_strsave(fname);
if (lp->sl_fname == NULL)
goto endFAIL;
// Check for .add.spl (_add.spl for VMS).
lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
}
else
lp = old_lp;
// Set sourcing_name, so that error messages mention the file name.
estack_push(ETYPE_SPELL, fname, 0);
ESTACK_CHECK_SETUP
did_estack_push = TRUE;
/*
* <HEADER>: <fileID>
*/
for (i = 0; i < VIMSPELLMAGICL; ++i)
buf[i] = (c = getc(fd)) == EOF ? 0 : c; // <fileID>
if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
{
emsg(_(e_this_does_not_look_like_spell_file));
goto endFAIL;
}
c = getc(fd); // <versionnr>
if (c < VIMSPELLVERSION)
{
emsg(_(e_old_spell_file_needs_to_be_updated));
goto endFAIL;
}
else if (c > VIMSPELLVERSION)
{
emsg(_(e_spell_file_is_for_newer_version_of_vim));
goto endFAIL;
}
/*
* <SECTIONS>: <section> ... <sectionend>
* <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
*/
for (;;)
{
n = getc(fd); // <sectionID> or <sectionend>
if (n == SN_END)
break;
c = getc(fd); // <sectionflags>
len = get4c(fd); // <sectionlen>
if (len < 0)
goto truncerr;
res = 0;
switch (n)
{
case SN_INFO:
lp->sl_info = read_string(fd, len); // <infotext>
if (lp->sl_info == NULL)
goto endFAIL;
break;
case SN_REGION:
res = read_region_section(fd, lp, len);
break;
case SN_CHARFLAGS:
res = read_charflags_section(fd);
break;
case SN_MIDWORD:
lp->sl_midword = read_string(fd, len); // <midword>
if (lp->sl_midword == NULL)
goto endFAIL;
break;
case SN_PREFCOND:
res = read_prefcond_section(fd, lp);
break;
case SN_REP:
res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
break;
case SN_REPSAL:
res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
break;
case SN_SAL:
res = read_sal_section(fd, lp);
break;
case SN_SOFO:
res = read_sofo_section(fd, lp);
break;
case SN_MAP:
p = read_string(fd, len); // <mapstr>
if (p == NULL)
goto endFAIL;
set_map_str(lp, p);
vim_free(p);
break;
case SN_WORDS:
res = read_words_section(fd, lp, len);
break;
case SN_SUGFILE:
lp->sl_sugtime = get8ctime(fd); // <timestamp>
break;
case SN_NOSPLITSUGS:
lp->sl_nosplitsugs = TRUE;
break;
case SN_NOCOMPOUNDSUGS:
lp->sl_nocompoundsugs = TRUE;
break;
case SN_COMPOUND:
res = read_compound(fd, lp, len);
break;
case SN_NOBREAK:
lp->sl_nobreak = TRUE;
break;
case SN_SYLLABLE:
lp->sl_syllable = read_string(fd, len); // <syllable>
if (lp->sl_syllable == NULL)
goto endFAIL;
if (init_syl_tab(lp) != OK)
goto endFAIL;
break;
default:
// Unsupported section. When it's required give an error
// message. When it's not required skip the contents.
if (c & SNF_REQUIRED)
{
emsg(_(e_unsupported_section_in_spell_file));
goto endFAIL;
}
while (--len >= 0)
if (getc(fd) < 0)
goto truncerr;
break;
}
someerror:
if (res == SP_FORMERROR)
{
emsg(_(e_format_error_in_spell_file));
goto endFAIL;
}
if (res == SP_TRUNCERROR)
{
truncerr:
emsg(_(e_truncated_spell_file));
goto endFAIL;
}
if (res == SP_OTHERERROR)
goto endFAIL;
}
// <LWORDTREE>
res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fbyts_len,
&lp->sl_fidxs, FALSE, 0);
if (res != 0)
goto someerror;
// <KWORDTREE>
res = spell_read_tree(fd, &lp->sl_kbyts, NULL, &lp->sl_kidxs, FALSE, 0);
if (res != 0)
goto someerror;
// <PREFIXTREE>
res = spell_read_tree(fd, &lp->sl_pbyts, NULL, &lp->sl_pidxs, TRUE,
lp->sl_prefixcnt);
if (res != 0)
goto someerror;
// For a new file link it in the list of spell files.
if (old_lp == NULL && lang != NULL)
{
lp->sl_next = first_lang;
first_lang = lp;
}
goto endOK;
endFAIL:
if (lang != NULL)
// truncating the name signals the error to spell_load_lang()
*lang = NUL;
if (lp != NULL && old_lp == NULL)
slang_free(lp);
lp = NULL;
endOK:
if (fd != NULL)
fclose(fd);
if (did_estack_push)
{
ESTACK_CHECK_NOW
estack_pop();
}
return lp;
}
/*
* Fill in the wordcount fields for a trie.
* Returns the total number of words.
*/
static void
tree_count_words(char_u *byts, idx_T *idxs)
{
int depth;
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
int c;
idx_T n;
int wordcount[MAXWLEN];
arridx[0] = 0;
curi[0] = 1;
wordcount[0] = 0;
depth = 0;
while (depth >= 0 && !got_int)
{
if (curi[depth] > byts[arridx[depth]])
{
// Done all bytes at this node, go up one level.
idxs[arridx[depth]] = wordcount[depth];
if (depth > 0)
wordcount[depth - 1] += wordcount[depth];
--depth;
fast_breakcheck();
}
else
{
// Do one more byte at this node.
n = arridx[depth] + curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
// End of word, count it.
++wordcount[depth];
// Skip over any other NUL bytes (same word with different
// flags).
while (byts[n + 1] == 0)
{
++n;
++curi[depth];
}
}
else
{
// Normal char, go one level deeper to count the words.
++depth;
arridx[depth] = idxs[n];
curi[depth] = 1;
wordcount[depth] = 0;
}
}
}
}
/*
* Load the .sug files for languages that have one and weren't loaded yet.
*/
void
suggest_load_files(void)
{
langp_T *lp;
int lpi;
slang_T *slang;
char_u *dotp;
FILE *fd;
char_u buf[MAXWLEN];
int i;
time_t timestamp;
int wcount;
int wordnr;
garray_T ga;
int c;
// Do this for all languages that support sound folding.
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
{
// Change ".spl" to ".sug" and open the file. When the file isn't
// found silently skip it. Do set "sl_sugloaded" so that we
// don't try again and again.
slang->sl_sugloaded = TRUE;
dotp = vim_strrchr(slang->sl_fname, '.');
if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
continue;
STRCPY(dotp, ".sug");
fd = mch_fopen((char *)slang->sl_fname, "r");
if (fd == NULL)
goto nextone;
/*
* <SUGHEADER>: <fileID> <versionnr> <timestamp>
*/
for (i = 0; i < VIMSUGMAGICL; ++i)
buf[i] = (c = getc(fd)) == EOF ? 0 : c; // <fileID>
if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
{
semsg(_(e_this_does_not_look_like_sug_file_str),
slang->sl_fname);
goto nextone;
}
c = getc(fd); // <versionnr>
if (c < VIMSUGVERSION)
{
semsg(_(e_old_sug_file_needs_to_be_updated_str),
slang->sl_fname);
goto nextone;
}
else if (c > VIMSUGVERSION)
{
semsg(_(e_sug_file_is_for_newer_version_of_vim_str),
slang->sl_fname);
goto nextone;
}
// Check the timestamp, it must be exactly the same as the one in
// the .spl file. Otherwise the word numbers won't match.
timestamp = get8ctime(fd); // <timestamp>
if (timestamp != slang->sl_sugtime)
{
semsg(_(e_sug_file_doesnt_match_spl_file_str),
slang->sl_fname);
goto nextone;
}
/*
* <SUGWORDTREE>: <wordtree>
* Read the trie with the soundfolded words.
*/
if (spell_read_tree(fd, &slang->sl_sbyts, NULL, &slang->sl_sidxs,
FALSE, 0) != 0)
{
someerror:
semsg(_(e_error_while_reading_sug_file_str),
slang->sl_fname);
slang_clear_sug(slang);
goto nextone;
}
/*
* <SUGTABLE>: <sugwcount> <sugline> ...
*
* Read the table with word numbers. We use a file buffer for
* this, because it's so much like a file with lines. Makes it
* possible to swap the info and save on memory use.
*/
slang->sl_sugbuf = open_spellbuf();
if (slang->sl_sugbuf == NULL)
goto someerror;
// <sugwcount>
wcount = get4c(fd);
if (wcount < 0)
goto someerror;
// Read all the wordnr lists into the buffer, one NUL terminated
// list per line.
ga_init2(&ga, 1, 100);
for (wordnr = 0; wordnr < wcount; ++wordnr)
{
ga.ga_len = 0;
for (;;)
{
c = getc(fd); // <sugline>
if (c < 0 || ga_grow(&ga, 1) == FAIL)
goto someerror;
((char_u *)ga.ga_data)[ga.ga_len++] = c;
if (c == NUL)
break;
}
if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
ga.ga_data, ga.ga_len, TRUE) == FAIL)
goto someerror;
}
ga_clear(&ga);
/*
* Need to put word counts in the word tries, so that we can find
* a word by its number.
*/
tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
nextone:
if (fd != NULL)
fclose(fd);
STRCPY(dotp, ".spl");
}
}
}
/*
* Read a length field from "fd" in "cnt_bytes" bytes.
* Allocate memory, read the string into it and add a NUL at the end.
* Returns NULL when the count is zero.
* Sets "*cntp" to SP_*ERROR when there is an error, length of the result
* otherwise.
*/
static char_u *
read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
{
int cnt = 0;
int i;
char_u *str;
// read the length bytes, MSB first
for (i = 0; i < cnt_bytes; ++i)
{
int c = getc(fd);
if (c == EOF)
{
*cntp = SP_TRUNCERROR;
return NULL;
}
cnt = (cnt << 8) + (unsigned)c;
}
*cntp = cnt;
if (cnt == 0)
return NULL; // nothing to read, return NULL
str = read_string(fd, cnt);
if (str == NULL)
*cntp = SP_OTHERERROR;
return str;
}
/*
* Read SN_REGION: <regionname> ...
* Return SP_*ERROR flags.
*/
static int
read_region_section(FILE *fd, slang_T *lp, int len)
{
int i;
int c = 0;
if (len > MAXREGIONS * 2)
return SP_FORMERROR;
for (i = 0; i < len; ++i)
lp->sl_regions[i] = (c = getc(fd)) == EOF ? 0 : c; // <regionname>
lp->sl_regions[len] = NUL;
return c == EOF ? SP_TRUNCERROR : 0;
}
/*
* Read SN_CHARFLAGS section: <charflagslen> <charflags>
* <folcharslen> <folchars>
* Return SP_*ERROR flags.
*/
static int
read_charflags_section(FILE *fd)
{
char_u *flags;
char_u *fol;
int flagslen, follen;
// <charflagslen> <charflags>
flags = read_cnt_string(fd, 1, &flagslen);
if (flagslen < 0)
return flagslen;
// <folcharslen> <folchars>
fol = read_cnt_string(fd, 2, &follen);
if (follen < 0)
{
vim_free(flags);
return follen;
}
// Set the word-char flags and fill SPELL_ISUPPER() table.
if (flags != NULL && fol != NULL)
set_spell_charflags(flags, flagslen, fol);
vim_free(flags);
vim_free(fol);
// When <charflagslen> is zero then <fcharlen> must also be zero.
if ((flags == NULL) != (fol == NULL))
return SP_FORMERROR;
return 0;
}
/*
* Read SN_PREFCOND section.
* Return SP_*ERROR flags.
*/
static int
read_prefcond_section(FILE *fd, slang_T *lp)
{
int cnt;
int i;
int n;
int c;
char_u *p;
char_u buf[MAXWLEN + 1];
// <prefcondcnt> <prefcond> ...
cnt = get2c(fd); // <prefcondcnt>
if (cnt <= 0)
return SP_FORMERROR;
lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
if (lp->sl_prefprog == NULL)
return SP_OTHERERROR;
lp->sl_prefixcnt = cnt;
for (i = 0; i < cnt; ++i)
{
// <prefcond> : <condlen> <condstr>
n = getc(fd); // <condlen>
if (n < 0 || n >= MAXWLEN)
return SP_FORMERROR;
// When <condlen> is zero we have an empty condition. Otherwise
// compile the regexp program used to check for the condition.
if (n > 0)
{
buf[0] = '^'; // always match at one position only
p = buf + 1;
while (n-- > 0)
*p++ = (c = getc(fd)) == EOF ? 0 : c; // <condstr>
if (c == EOF)
break;
*p = NUL;
lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
}
}
return 0;
}
/*
* Read REP or REPSAL items section from "fd": <repcount> <rep> ...
* Return SP_*ERROR flags.
*/
static int
read_rep_section(FILE *fd, garray_T *gap, short *first)
{
int cnt;
fromto_T *ftp;
int i;
cnt = get2c(fd); // <repcount>
if (cnt < 0)
return SP_TRUNCERROR;
if (ga_grow(gap, cnt) == FAIL)
return SP_OTHERERROR;
// <rep> : <repfromlen> <repfrom> <reptolen> <repto>
for (; gap->ga_len < cnt; ++gap->ga_len)
{
ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
ftp->ft_from = read_cnt_string(fd, 1, &i);
if (i < 0)
return i;
if (i == 0)
return SP_FORMERROR;
ftp->ft_to = read_cnt_string(fd, 1, &i);
if (i <= 0)
{
vim_free(ftp->ft_from);
if (i < 0)
return i;
return SP_FORMERROR;
}
}
// Fill the first-index table.
for (i = 0; i < 256; ++i)
first[i] = -1;
for (i = 0; i < gap->ga_len; ++i)
{
ftp = &((fromto_T *)gap->ga_data)[i];
if (first[*ftp->ft_from] == -1)
first[*ftp->ft_from] = i;
}
return 0;
}
/*
* Read SN_SAL section: <salflags> <salcount> <sal> ...
* Return SP_*ERROR flags.
*/
static int
read_sal_section(FILE *fd, slang_T *slang)
{
int i;
int cnt;
garray_T *gap;
salitem_T *smp;
int ccnt;