File: update-ispell-hash.dpatch

package info (click to toggle)
ipolish 1.0.990914-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,832 kB
  • sloc: perl: 1,363; sh: 4
file content (333 lines) | stat: -rw-r--r-- 9,950 bytes parent folder | download
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
--- /dev/null	Tue Jul 21 02:45:36 1998
+++ update-ispell-hash	Mon Sep 13 18:02:23 1999
@@ -0,0 +1,330 @@
+#! /usr/bin/perl
+# update-ispell-hash 0.4 -- creating dictionary hash from subdictionaries
+#
+# (c) 1999 Piotr Roszatycki <dexter@debian.org>
+#
+# Some subroutines taken from yada 0.6
+# Copyright 1999 Charles Briscoe-Smith
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# Changes:
+#  * 0.4 - bugfix, "Can't open file" if file didn't exist
+#  * 0.3 - renamed dictionary.info -> dictionary.desc
+#        - gzip, bzip2 and sq support
+#        - fixed error messages
+#        - new options
+#        - build process uses icombine
+#  * 0.2 - security check for temporary file
+#  * 0.1 - initial release
+#
+# Todo:
+# * clean code?
+
+$ispelllibpath = "/usr/lib/ispell";
+$ispellvarpath = "/var/lib/ispell";
+$ispellsharepath = "/usr/share/ispell";
+$tmppath = "/tmp/ispell-config.$$";
+
+
+
+%sourcefields = ();
+foreach ("source", "affix", "hash")
+{
+  $sourcefields{$_}++;
+}
+
+%dictionaryfields = ();
+foreach ("dictionary", "description", "default")
+{
+  $dictionaryfields{$_}++;
+}
+
+########################################################################
+# Standard ways of printing error messages
+########################################################################
+sub gasp {
+  print STDERR "@_\n";
+  $errors++;
+}
+
+sub choke {
+  print STDERR "@_\n";
+  &clean();
+  exit 1;
+}
+
+########################################################################
+# About info
+########################################################################
+sub about {
+    choke "Usage:\tupdate-ispell-hash [-h] [-a] [-d] [-f <file>] [-x <file>] [<dictionary>]\n"
+}
+
+sub usage {
+    choke "Usage:\tupdate-ispell-hash [-h] [-a] [-d] [-f <file>] [-x <file>] [<dictionary>]\n"
+         ."\n"
+         ."Options:\n"
+         ."\t-h | --help           this help info\n"
+         ."\t-a | --all            build from all dictionaries\n"
+         ."\t-d | --default        build from default dictionaries\n"
+         ."\t-f | --file <file>    build hash to file\n"
+         ."\t-x | --affix <file>   use affix file\n"
+         ."\t<dictionary>          dictionary to building\n";
+}
+
+########################################################################
+# Execute an external program, and bomb out if errors occur
+########################################################################
+sub run {
+  if (my $pid = fork) {
+    waitpid $pid, 0;
+    choke "*** error: $_[0] exited with status $?\n" if $? != 0;
+  } elsif (defined $pid) {
+    exec @_ or exit 1;
+  } else {
+    choke "Cannot fork a child process";
+  }
+}
+
+########################################################################
+# Read a paragraph into %par
+########################################################################
+sub getpara {
+  return 0 if eof DESC;
+  while (<DESC>) {
+    next if m/^\s*\#/;
+    s/\s+$//;
+    last unless m/^$/;
+  }
+  %par=();
+  while (defined) {
+    m/^([-A-Za-z0-9]+)\s*\:\s*(.*)$/ or choke "Invalid line found";
+    $keyword=$1;
+    $keyword=~tr/A-Z/a-z/;
+    $contents=$2;
+    while (<DESC>) {
+      next if m/^\#/;
+      s/\s+$//s;
+      last unless s/^(\s)//;
+      $x=$1;
+      s/^/$x/ if $x ne " ";
+      s/^\.(\.*)$/$1/;
+      $contents.="\n".$_;
+    }
+    $contents=~s/^\n//s;
+    $par{$keyword}=$contents;
+    last if not defined or m/^$/;
+  }
+  if( ! defined %source ) {
+    foreach (keys %par) {
+      choke "Unknown field `$_' in source paragraph"
+        unless exists $sourcefields{$_};
+    }
+    %source = %par;
+  } else {
+    foreach (keys %par) {
+      choke "Unknown field `$_' in dictionary paragraph"
+        unless exists $dictionaryfields{$_};
+    }
+    $dictionary{$par{dictionary}} = %par;
+  }
+  return 1;
+}
+
+########################################################################
+# Clean temporary files
+########################################################################
+sub clean {
+    unlink <$tmppath*>;
+}
+
+########################################################################
+# Signal trap
+########################################################################
+sub trap {
+    &clean();
+    exit 1;
+}
+
+########################################################################
+# Main program
+########################################################################
+
+$SIG{INT}  = \&trap;
+$SIG{KILL} = \&trap;
+$SIG{TERM} = \&trap;
+
+while( $_ = shift @ARGV ) {
+    if( $_ eq "-h" || $_ eq "--help" ) {
+	&usage();
+    } elsif( $_ eq "-a" || $_ eq "--all" ) {
+	$all = 1;
+    } elsif( $_ eq "-d" || $_ eq "--default" ) {
+	$default = 1;
+    } elsif( $_ eq "-f" || $_ eq "--file" ) {
+	$ispellhashfile = shift @ARGV;
+    } elsif( $_ eq "-x" || $_ eq "--affix" ) {
+	$ispellaffixfile = shift @ARGV;
+    } elsif( $_ eq "-l" || $_ eq "--list" ) {
+	$ispelllistfile = shift @ARGV;
+    } elsif( $_ =~ /^-/ || $source ) {
+	&about();
+    } else {
+	$source = $_;
+    }
+}
+
+unless( $source ) {
+    print "\nPlease choose dictionary:\n\n";
+    foreach( <$ispellsharepath/*.desc> ) {
+	s/.*\///;
+	s/\.desc//;
+	$source[$i++] = $_;
+	print "\t[$i] $_\n";
+    }
+    choke "Can't find any $ispellsharepath/*.desc file" unless $i;
+    print "\nSelect the number of dictionary [1] ";
+    $source = <STDIN>;
+    chomp $source;
+    if( $source =~ /^\d+$/ ) {
+	$source = $source[$source-1];
+    } elsif ( $source eq "" ) {
+	$source = $source[0];
+    }
+}
+
+-d "$ispellsharepath/$source" or
+    choke "Can't find $ispellsharepath/$source dictionary\n";
+open DESC, "$ispellsharepath/$source.desc" or
+    choke "Can't open $ispellsharepath/$source.desc file for reading";
+
+open WORDS, ">$tmppath.words" or
+    choke "Can't open $tmppath.words file for writing";
+
+&getpara();
+
+choke "Source: field not found\n" unless $source{source};
+choke "Affix: field not found\n" unless $source{affix};
+choke "Hash: field not found\n" unless $source{hash};
+
+$ispellaffixfile = $source{affix} unless $ispellaffixfile;
+$ispellaffixfile = "$ispelllibpath/$ispellaffixfile" unless $ispellaffixfile =~ /\//;
+$ispellhashfile = $source{hash} unless $ispellhashfile;
+$ispellhashfile = "$ispellvarpath/$ispellhashfile" unless $ispellhashfile =~ /\//;
+$ispellhashfile = "$ispellhashfile.hash" unless $ispellhashfile =~ /\.hash$/;
+$ispelllistfile = "$source" unless $ispelllistfile;
+$ispelllistfile = "$ispellvarpath/$ispelllistfile" unless $ispelllistfile =~ /\//;
+$ispelllistfile = "$ispelllistfile.list" unless $ispelllistfile =~ /\.list$/;
+
+# check if hash file is writable
+if( ! -w $ispellhashfile ) {
+    open TMP, ">$ispellhashfile" or
+	choke "Can't write $ispellhashfile file";
+    close TMP;
+    unlink $ispellhashfile;
+}
+
+print "\nPlease choose dictionary modules for $source hash\n\n"
+    unless( $all || $default );
+
+if( -f $ispelllistfile ) {
+    open LIST, "$ispelllistfile" or
+	choke "Can't open $ispelllistfile for reading";
+    while( <LIST> ) {
+	chomp;
+	$default{$_} = 1;
+    }
+    $list = 1;
+}
+
+while(&getpara()) {
+    if( ! $list ) {	
+	$default{$par{dictionary}} = 1 if $par{default};
+    }
+    if( $all ) {
+	$opt = "y";
+    } elsif( $default ) {
+	$opt = "";
+    } else {
+	$desc = $par{description};
+	$desc =~ s/\n.*//s;
+	printf "$desc? [%s/%s/i/q] ", $default{$par{dictionary}} ? "Y" : "y", 
+	    $default{$par{dictionary}} ? "n" : "N";
+	$opt = <STDIN>;
+	chomp $opt;
+    }
+    if( lc($opt) eq "y" || $opt eq "" && $default{$par{dictionary}} ) {
+	$list{$par{dictionary}} = 1;
+	$file = "$ispellsharepath/$source/$par{dictionary}";
+	if( -f "$file" ) {
+            open FILE, "$file" or
+		choke "Can't open $file file for reading";
+	} elsif( -f "$file.sq" ) {
+            open FILE, "unsq $file.sq |" or
+		choke "Can't open $file.sq file for reading";
+	} elsif( -f "$file.gz" ) {
+            open FILE, "gzip -cd $file.gz |" or
+		choke "Can't open $file.gz file for reading";
+	} elsif( -f "$file.sq.gz" ) {
+            open FILE, "gzip -cd $file.sq.gz | unsq |" or
+		choke "Can't open $file.sq.gz file for reading";
+	} elsif( -f "$file.bz2" ) {
+            open FILE, "bzip2 -cd $file.bz2 |" or
+		choke "Can't open $file.bz2 file for reading";
+	} elsif( -f "$file.sq.bz2" ) {
+            open FILE, "bzip2 -cd $file.sq.bz2 | unsq |" or
+		choke "Can't open $file.sq.bz2 file for reading";
+	} else {
+	    choke "Can't find $file dictionary";
+	}
+	while( <FILE> ) {
+	    print WORDS;
+	}
+	close FILE;
+    } elsif( lc($opt) eq "i" ) {
+	$desc = $par{description};
+	$desc =~ s/^[^\n]*\n?//s;
+	print $desc, "\n\n";
+	redo;
+    } elsif( lc($opt) eq "q" ) {
+	print "Abort.\n";
+	&clean();
+	exit 0;
+    }
+}
+
+close DICT;
+close WORDS;
+close LIST;
+
+if( ! $default && ! $all ) {
+    open LIST, ">$ispelllistfile" or
+	choke "Can't open $ispelllistfile for writing";
+    foreach( sort keys %list ) {
+	print LIST "$_\n";
+    }
+    close LIST;
+}
+
+-e "$tmppath.dict" and die "$tmppath.dict already exists\n";
+&run("cat $tmppath.words | tr -d \'\\r\' | grep -v \"^/\" | ".
+     "sort | icombine $ispellaffixfile > $tmppath.dict");
+&run("buildhash $tmppath.dict $ispellaffixfile $ispellhashfile 2>&1");
+print "Done.\n";
+
+&clean();
+
+1;