OSDN Git Service

2004-06-25 Artem B. Bityuckiy <dedekind@oktetlabs.ru>
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / iconv / ces / mkdeps.pl
1 #!/usr/bin/perl -w
2 #
3 #  Copyright (c) 2003-2004, Artem B. Bityuckiy, SoftMine Corporation.
4 #
5 #  Redistribution and use in source and binary forms, with or without
6 #  modification, are permitted provided that the following conditions
7 #  are met:
8 #  1. Redistributions of source code must retain the above copyright
9 #     notice, this list of conditions and the following disclaimer.
10 #  2. Redistributions in binary form must reproduce the above copyright
11 #     notice, this list of conditions and the following disclaimer in the
12 #     documentation and/or other materials provided with the distribution.
13 #
14 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 #  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 #  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 #  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 #  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 #  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 #  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 #  SUCH DAMAGE.
25 #
26 use integer;
27 use Getopt::Std;
28 use strict;
29
30 sub err($);
31 sub process_section_encodings($);
32 sub process_section_cesdeps($);
33 sub next_entry($$$);
34
35 sub generate_cesbi_h($$);
36 sub generate_encnames_h(@);
37 sub generate_aliasesbi_c($);
38 sub generate_encoding_aliases_c($);
39 sub generate_cesdeps_h($);
40 sub generate_ccsbi_h($);
41 sub generate_cesbi_c($);
42 sub generate_ccsnames_h($);
43
44 # ==============================================================================
45 #
46 # GLOBAL VARIABLES
47 #
48 # ==============================================================================
49
50 my $comment_automatic =
51 "/*
52  * This file was automatically generated mkdeps.pl script. Don't edit.
53  */";
54
55 my $macro_from_enc     = '_ICONV_FROM_ENCODING_';
56 my $macro_to_enc       = '_ICONV_TO_ENCODING_';
57 my $macro_from_ucs_ces = 'ICONV_FROM_UCS_CES_';
58 my $macro_to_ucs_ces   = 'ICONV_TO_UCS_CES_';
59 my $macro_from_ucs_ccs = 'ICONV_FROM_UCS_CCS_';
60 my $macro_to_ucs_ccs   = 'ICONV_TO_UCS_CCS_';
61 my $macro_enc_name     = 'ICONV_ENCODING_';
62 my $macro_ccs_name     = 'ICONV_CCS_';
63
64 my $var_from_ucs_handlers = '_iconv_from_ucs_ces_handlers_';
65 my $var_to_ucs_handlers   = '_iconv_to_ucs_ces_handlers_';
66 my $var_ccs       = '_iconv_ccs_';
67 my $var_aliases   = '_iconv_aliases';
68 my $var_ces_names = 'iconv_ces_names_';
69
70 # ==============================================================================
71 #
72 # PARSE COMMAND-LINE OPTIONS.
73 #
74 # ==============================================================================
75
76 my %options;
77
78 # SUPPORTED OPTIONS.
79 my $help_opt    = 'h';
80 my $infile_opt  = 'i';
81 my $verbose_opt = 'v';
82
83 # Default input configuration file name
84 my $default_infile = '../lib/encoding.deps';
85 # Real input configuration file name
86 my $infile;
87 # Verbose flag (be verbose if not zero)
88 my $verbose;
89
90 {
91 getopts ("${help_opt}${verbose_opt}${infile_opt}:", \%options)
92 or err "getopts() failed: $!.";
93
94 if ($options{$help_opt})
95 {
96   # Output help message and exit.
97   print "Usage: $0 [-$infile_opt depfile] [-$help_opt]\n";
98   print "\t-$infile_opt - input file with configuration ($default_infile ";
99   print "file will be used by default)\n";
100   print "\t-$help_opt - this help message\n";
101   exit 0;
102 }
103
104 # Input file name.
105 $infile = $options{$infile_opt} ? $options{$infile_opt} : $default_infile;
106 $verbose = $options{$verbose_opt} ? 1 : 0;
107
108 print "Debug: -$verbose_opt option found.\n" if $verbose;
109
110 # ==============================================================================
111 #
112 # Find and fetch sections from input file
113 #
114 # ==============================================================================
115
116 # Opening input file
117 print "Debug: open \"$infile\" input file.\n" if $verbose;
118 open (INFILE, '<', $infile) or err "Can't open \"$infile\" file for reading.\n"
119                                  . "System error message: $!.\n";
120
121 # Configuration file markers
122 my $marker_section = 'SECTION';
123 my $marker_section_end = 'SECTION END';
124
125 # File sections. Hash values are references to arrays with section contents
126 my %sections;
127
128 # Extract sections from file
129 for (my $ln = 1; my $l = <INFILE>; $ln += 1)
130 {
131   # Skip comments and empty lines
132   next if $l =~ m/^#.*$/ or $l =~ m/^\s*$/;
133
134   # Remove last CR symbol
135   $l =~ s/^(.*)\n$/$1/, $l =~ s/^(.*)\r$/$1/;
136
137   # Generate error if line isn't section begin marker
138   err "(input file line $ln) Unexpected marker: \"$l\". ${marker_section} "
139     . "is expected."
140   if $l !~ m/^$marker_section(\s+(\S*)\s*)?$/;
141   
142   # Generate error if there is no section name
143   err "(input file line $ln) Section name isn't found"
144   if !$1 or !$2;
145   
146   # Generate error if this is section end marker
147   err "(input file line $ln) Unexpected \"${marker_section_end}\" marker "
148     . "in input file."
149   if $2 eq $marker_section_end;
150
151   my $sect_name = $2;
152
153   # Extract section content
154   for (; $l = <INFILE>; $ln += 1)
155   {
156     # Skip comments and empty lines
157     next if $l =~ m/^#.*$/ or $l =~ m/^$/;
158     # Remove last CR symbol
159     $l =~ s/^(.*)\n$/$1/, $l =~ s/^(.*)\r$/$1/;
160     
161     last if $l =~ m/^$marker_section_end$/;
162
163     push @{$sections{$sect_name}}, $l;
164   }
165
166   # Generate error if section wasn't ended
167   err "(input file line $ln) \"No $marker_section_end\" marker found"
168   if $l !~ m/^$marker_section_end$/;
169 }
170
171 close INFILE or err "Error while closing input file.";
172
173 # =============================================================================
174 #
175 # Now sections are fetched. Each section is processed by separate function.
176 # There are only three supported sections now: ENCODINGS, CES_DEPENDENCIES
177 # and ENCODING_CCS_DEPENDENCIES.
178 #
179 # =============================================================================
180
181 my $section_encodings = 'ENCODINGS';
182 my $section_cesdeps   = 'CES_DEPENDENCIES';
183
184 my $section;
185
186 err "$section_encodings not found."
187 if !defined $sections{$section_encodings};
188 err "$section_cesdeps not found."
189 if !defined $sections{$section_cesdeps};
190
191 # Process sections
192 print "Debug: process $section_encodings section.\n" if $verbose;
193 process_section_encodings ($sections{$section_encodings});
194 delete $sections{$section_encodings};
195
196 print "Debug: process $section_cesdeps section.\n" if $verbose;
197 process_section_cesdeps ($sections{$section_cesdeps});
198 delete $sections{$section_cesdeps};
199
200 print STDERR "Warning: section \"$_\" was ignored!\n"
201 foreach (keys %sections);
202
203 exit 1;
204 }
205
206 # =============================================================================
207 #
208 # Print error message and exit.
209 #
210 # Parameter 1: error message.
211 #
212 # =============================================================================
213 sub err($)
214 {
215   print STDERR "Error while running script.\n$_[0]\n";
216   exit 0;
217 }
218
219
220 # =============================================================================
221 #
222 # Process ENCODINGS section.
223 #
224 # Parameter 1 (input):  array reference with section content;
225 #
226 # =============================================================================
227 sub process_section_encodings($)
228 {
229   my $sect = $_[0];
230   my $lineidx = 0;
231   my @entry;
232   my $marker_encoding = 'ENCODING';
233   my $marker_ces      = 'CES';
234   my $marker_ccs      = 'CCS';
235   my $marker_aliases  = 'ALIASES';
236
237   # Keys: CES names. Values: array reference with encodings list.
238   my %cesenc;
239   # Keys: encodings. Values: CES converter names.
240   my %encces;
241   # Keys: CCS tables names. Values: array reference with encodings.
242   my %ccsenc;
243   # Keys: encodings. Values: aliases list.
244   my %encalias;
245
246   while (next_entry ($sect, \@entry, \$lineidx))
247   {
248     my $encoding;
249     my $ces;
250     my $ccs;
251     my $aliases;
252     
253     foreach my $l (@entry)
254     {
255       if ($l =~ m/^($marker_encoding):\s*(\S*)\s*$/)
256       {
257         err "(process_section_encodings()) More than one $marker_encoding "
258           . "records found ($l)"
259         if defined $encoding;
260         
261         $encoding = $2; 
262       }
263       elsif ($l =~ m/^($marker_ces):\s*(\S*)\s*$/)
264       {
265         err "(process_section_encodings()) More than one $marker_ces "
266           . "records found ($l)"
267         if defined $ces;
268
269         $ces = $2;
270       }
271       elsif ($l =~ m/^($marker_aliases):\s*(.*)\s*$/)
272       {
273         err "(process_section_encodings()) More than one "
274           . "$marker_aliases records found ($l)"
275         if defined $aliases;
276
277         $aliases = $2;
278       }
279       elsif ($l =~ m/^($marker_ccs):\s*(.*)\s*$/)
280       {
281         err "(process_section_encodings()) More than one "
282           . "$marker_ccs records found ($l)"
283         if defined $ccs;
284
285         $ccs = $2;
286       }
287       else
288       {
289         err "(process_section_encodings()) Can't parse \"$l\"";
290       }
291     }
292   
293     err "(process_section_encodings()) $encoding is defined twice"
294     if (defined $encces{$encoding});
295     err "(process_section_encodings()) ENCODING: field isn't found"
296     if not defined $encoding;
297
298     if (defined $ces)
299     {
300       push @{$cesenc{$ces}}, $encoding;
301       $encces{$encoding} = $ces;
302     }
303     
304     if (defined $ccs)
305     {
306       my @ccs = split / /, $ccs;
307       push @{$ccsenc{$_}}, $encoding foreach (@ccs);
308     }
309     $encalias{$encoding} = $aliases;
310   }
311
312   # Generate cesbi.h header file
313   generate_cesbi_h (\%cesenc, \%encces);
314
315   # Generate encnames.h header file
316   generate_encnames_h (keys %encces);
317
318   # Generate aliasesbi.c file
319   generate_aliasesbi_c (\%encalias);
320   
321   # Generate encoding.aliases file
322   generate_encoding_aliases (\%encalias);
323
324   # Generate ccsbi.h header file
325   generate_ccsbi_h (\%ccsenc);
326
327   # Generate cesbi.c file
328   generate_cesbi_c (\%cesenc);
329
330   # Generate ccsbi.c file
331   my @ccs = keys %ccsenc;
332   generate_ccsbi_c (\@ccs);
333   
334   # Generate ccsnames.h header file
335   generate_ccsnames_h (\%ccsenc);
336
337 }
338
339 # ==============================================================================
340 #
341 # Process CES_DEPENDENCIES section.
342 #
343 # Parameter 1: array reference with section content.
344 #
345 # ==============================================================================
346 sub process_section_cesdeps($)
347 {
348   my $sect = $_[0];
349   my $lineidx = 0;
350   my @entry;
351   my $marker_ces      = 'CES';
352   my $marker_used_ces = 'USED_CES';
353   my %cesdeps;
354
355   while (next_entry ($sect, \@entry, \$lineidx))
356   {
357     my $ces;
358     my $used_ces;
359
360     foreach my $l (@entry)
361     {
362       if ($l =~ m/^($marker_ces):\s*(\S*)\s*$/)
363       {
364         err "(process_section_cesdeps()) More than one $marker_ces "
365           . "records found ($l)"
366         if $ces;
367         
368         $ces = $2; 
369       }
370       elsif ($l =~ m/^($marker_used_ces):\s*(.*)\s*$/)
371       {
372         err "(process_section_cesdeps()) More than one $marker_used_ces "
373           . "records found ($l)"
374         if $used_ces;
375
376         $used_ces = $2;
377       }
378       else
379       {
380         err "(process_section_cesdeps()) Can't parse \"$l\"";
381       }
382     }
383   
384     err "(process_section_esdeps()) $ces dependecties are defined twice"
385     if (defined $cesdeps{$ces});
386
387     # Split string
388     my @used_ces = split / /, $used_ces;
389
390     $cesdeps{$ces} = \@used_ces;
391   }
392
393   # Generate cesdeps.h header file
394   generate_cesdeps_h (\%cesdeps);
395 }
396
397 # ==============================================================================
398 #
399 # Extract next entry.
400 #
401 # Parameter 1 (input): array reference with entries;
402 # Parameter 2 (output): array reference with entry content;
403 # Parameter 3 (input/output): scalar reference with line index to process.
404 #
405 # Returns 1 is entry was found, 0 if thee is no more entries;
406 #
407 # ==============================================================================
408 sub next_entry($$$)
409 {
410   my $entries = $_[0];
411   my $entry   = $_[1];
412   my $idx     = $_[2];
413   my $marker_entry = 'ENTRY';
414   my $marker_entry_end = 'ENTRY END';
415   my $entry_flag = 0;
416
417   return 0 if not defined ${$entries}[${$idx}];
418
419   undef @{$entry};
420
421   for (; my $l = ${$entries}[${$idx}++];)
422   {
423     # Skip comments and empty lines
424     next if $l =~ m/^#.*$/ or $l =~ m/^\s*$/;
425     
426     if ($l =~ m/^$marker_entry$/)
427     {
428       err "(next_entry()) $marker_entry marker appears twice"
429       if ($entry_flag == 1);
430       $entry_flag = 1;
431       $l = ${$entries}[${$idx}++]
432     }
433     else
434     {
435       # Generate error if line isn't entry begin marker
436       err "(next_entry()) Unexpected marker: \"$l\". ${marker_entry} "
437         . "is expected."
438       if ($entry_flag == 0)
439     }
440         
441     last if $l =~ m/^$marker_entry_end$/;
442
443     push @{$entry}, $l;
444   }
445
446   return 1;
447 }
448
449 # ==============================================================================
450 #
451 # Generate cesbi.h file.
452 #
453 # Parameter 1 (input): hash reference with keys = CES Converters names and
454 # values = array references with list of supported encodings.
455 # Parameter 2 (input): hash reference with keys = encodings names and
456 # values = CES converter names.
457 #
458 # ==============================================================================
459 sub generate_cesbi_h($$)
460 {
461   my %cesenc = %{$_[0]};
462   my %encces = %{$_[1]};
463   my @ces = sort keys %cesenc;
464   
465   print "Debug: create \"cesbi.h\" file.\n" if $verbose;
466   open (CESBI_H, '>', "cesbi.h")
467   or err "Can't create \"cesbi.h\" file for writing.\nSystem error message: $!.\n";
468
469   print CESBI_H "$comment_automatic\n\n";
470   print CESBI_H "#ifndef __CESBI_H__\n";
471   print CESBI_H "#define __CESBI_H__\n\n";
472   print CESBI_H "#include <newlib.h>\n";
473   print CESBI_H "#include <_ansi.h>\n";
474   print CESBI_H "#include \"../lib/encnames.h\"\n";
475   print CESBI_H "#include \"../lib/ucsconv.h\"\n\n";
476   print CESBI_H "/*\n";
477   print CESBI_H " * Enable CES converter if correspondent encoding is requested.\n";
478   print CESBI_H " * Defining ${macro_to_ucs_ces}XXX macro or ${macro_from_ucs_ces}XXX\n";
479   print CESBI_H " * macro is needed to enable \"XXX encoding -> UCS\" or \"UCS -> XXX encoding\"\n";
480   print CESBI_H " * part of UCS-based CES converter.\n";
481   print CESBI_H " */\n";
482     
483   foreach my $ces (@ces)
484   {
485     my @encs = sort @{$cesenc{$ces}};
486     foreach my $encoding (@encs)
487     {
488       print CESBI_H $encoding eq $encs[0] ? "#if " : " || ";
489       print CESBI_H "defined ($macro_from_enc\U$encoding)";
490       print CESBI_H " \\" if $encoding ne $encs[$#encs];
491       print CESBI_H "\n";
492     }
493     print CESBI_H "#  define $macro_to_ucs_ces\U$ces\n";
494     print CESBI_H "#endif\n";
495     
496     foreach my $encoding (@encs)
497     {
498       print CESBI_H $encoding eq $encs[0] ? "#if " : " || ";
499       print CESBI_H "defined ($macro_to_enc\U$encoding)";
500       print CESBI_H " \\" if $encoding ne $encs[$#encs];
501       print CESBI_H "\n";
502     }
503     print CESBI_H "#  define $macro_from_ucs_ces\U$ces\n";
504     print CESBI_H "#endif\n\n";
505   }
506   
507   print CESBI_H "/*\n";
508   print CESBI_H " * Some encodings require another encodings to be enabled.\n";
509   print CESBI_H " * These dependencies are handled in cesdeps.h header file.\n";
510   print CESBI_H " */\n";
511   print CESBI_H "#include \"cesdeps.h\"\n\n";
512
513   print CESBI_H "/*\n";
514   print CESBI_H " * NLS uses iconv's capabilities and require one of encodings\n";
515   print CESBI_H " * to be enabled for internal wchar_t representation.\n";
516   print CESBI_H " */\n";
517   print CESBI_H "#include \"../lib/iconvnls.h\"\n\n";
518
519   print CESBI_H "/*\n";
520   print CESBI_H " * Forward declarations of CES converter handlers.\n";
521   print CESBI_H " * These handlers are actually defined in correspondent CES converter files.\n";
522   print CESBI_H " */\n";
523
524   foreach my $ces (@ces)
525   {
526     print CESBI_H "#ifdef $macro_to_ucs_ces\U$ces\n";
527     print CESBI_H "extern _CONST iconv_to_ucs_ces_handlers_t\n";
528     print CESBI_H "$var_to_ucs_handlers$ces;\n";
529     print CESBI_H "#endif\n";
530
531     print CESBI_H "#ifdef $macro_from_ucs_ces\U$ces\n";
532     print CESBI_H "extern _CONST iconv_from_ucs_ces_handlers_t\n";
533     print CESBI_H "$var_from_ucs_handlers$ces;\n";
534     print CESBI_H "#endif\n\n";
535   }
536
537   print CESBI_H "#endif /* !__CESBI_H__ */\n\n";
538   close CESBI_H or err "Error while closing cesbi.h file.";
539 }
540
541 # ==============================================================================
542 #
543 # Generate encnames.h header file.
544 #
545 # Parameters: array of supported encodings.
546 #
547 # ==============================================================================
548 sub generate_encnames_h(@)
549 {
550   print "Debug: create \"../lib/encnames.h\" file.\n" if $verbose;
551   open (ENCNAMES_H, '>', "../lib/encnames.h")
552   or err "Can't create \"../lib/encnames.h\" file for writing.\nSystem error message: $!.\n";
553
554   print ENCNAMES_H "$comment_automatic\n\n";
555   print ENCNAMES_H "#ifndef __ENCNAMES_H__\n";
556   print ENCNAMES_H "#define __ENCNAMES_H__\n\n";
557
558   print ENCNAMES_H "/*\n";
559   print ENCNAMES_H " * Encodings name macros.\n";
560   print ENCNAMES_H " */\n";
561   
562   foreach my $enc (sort @_)
563   {
564     print ENCNAMES_H "#define $macro_enc_name\U$enc\E \"$enc\"\n";
565   }
566
567   print ENCNAMES_H "\n#endif /* !__ENCNAMES_H__ */\n\n";
568   close ENCNAMES_H or err "Error while closing ../lib/encnames.h file.";
569 }
570
571 # ==============================================================================
572 #
573 # Generate aliasesbi.c C source file.
574 #
575 # Parameters: hash reference with keys = encodings and values = aliases string.
576 #
577 # ==============================================================================
578 sub generate_aliasesbi_c($)
579 {
580   print "Debug: create \"../lib/aliasesbi.c\" file.\n" if $verbose;
581   open (ALIASESBI_C, '>', "../lib/aliasesbi.c")
582   or err "Can't create \"../lib/aliasesbi.c\" file for writing.\nSystem error message: $!.\n";
583
584   print ALIASESBI_C "$comment_automatic\n\n";
585   print ALIASESBI_C "#include <_ansi.h>\n";
586   print ALIASESBI_C "#include \"encnames.h\"\n\n";
587   print ALIASESBI_C "_CONST char *\n";
588   print ALIASESBI_C "$var_aliases =\n";
589   print ALIASESBI_C "{\n";
590
591   foreach my $enc (sort keys %{$_[0]})
592   {
593     print ALIASESBI_C "#if defined ($macro_from_enc\U$enc) \\\n";
594     print ALIASESBI_C " || defined ($macro_to_enc\U$enc)\n";
595     print ALIASESBI_C "  $macro_enc_name\U$enc\E";
596     print ALIASESBI_C " \" ${$_[0]}{$enc}\\n\"" if defined ${$_[0]}{$enc};
597     print ALIASESBI_C "\n";
598     print ALIASESBI_C "#endif\n";
599   }
600   print ALIASESBI_C "  \"\"\n";
601   print ALIASESBI_C "};\n\n";
602   
603   close ALIASESBI_C or err "Error while closing ../lib/aliasesbi.c file.";
604 }
605
606 # ==============================================================================
607 #
608 # Generate encoding.aliases file.
609 #
610 # Parameter 1: hash reference with keys = encodings and values = aliases string.
611 #
612 # ==============================================================================
613 sub generate_encoding_aliases($)
614 {
615   print "Debug: create \"../encoding.aliases\" file.\n" if $verbose;
616   open (ALIASES, '>', "../encoding.aliases")
617   or err "Can't create \"../encoding.aliases\" file for writing.\nSystem error message: $!.\n";
618
619   print ALIASES "#\n# This file was automatically generated. Don't edit.\n#\n\n";
620
621   foreach my $enc (sort keys %{$_[0]})
622   {
623     print ALIASES "$enc";
624     print ALIASES " ${$_[0]}{$enc}" if defined ${$_[0]}{$enc};
625     print ALIASES "\n";
626   }
627   
628   print ALIASES "\n";
629   
630   close ALIASES or err "Error while closing ./encoding.aliases file.";
631 }
632
633 # ==============================================================================
634 #
635 # Generate cesdeps.h header file.
636 #
637 # Parameter 1: hash reference with keys = CES converters and values = references
638 # to arrays with list of CES converters which are needed by that CES converter
639 # (defined by key).
640 #
641 # ==============================================================================
642 sub generate_cesdeps_h($)
643 {
644   my %cesdeps = %{$_[0]};
645   
646   print "Debug: create \"cesdeps.h\" file.\n" if $verbose;
647   open (CESDEPS_H, '>', "cesdeps.h")
648   or err "Can't create \"cesdeps.h\" file for writing.\nSystem error message: $!.\n";
649
650   print CESDEPS_H "$comment_automatic\n\n";
651   print CESDEPS_H "#ifndef __CESDEPS_H__\n";
652   print CESDEPS_H "#define __CESDEPS_H__\n\n";
653
654   print CESDEPS_H "/*\n";
655   print CESDEPS_H " * Some CES converters use another CES converters and the following\n";
656   print CESDEPS_H " * is such dependencies description.\n";
657   print CESDEPS_H " */\n";
658   
659   foreach my $ces (sort keys %cesdeps)
660   {
661     my @deps = sort @{$cesdeps{$ces}};
662
663     print CESDEPS_H "#ifdef $macro_to_ucs_ces\U$ces\n";
664     
665     foreach my $dep (@deps)
666     {
667       print CESDEPS_H "#  ifndef $macro_to_ucs_ces\U$dep\n";
668       print CESDEPS_H "#    define $macro_to_ucs_ces\U$dep\n";
669       print CESDEPS_H "#  endif\n";
670     }
671     print CESDEPS_H "#endif\n";
672     
673     print CESDEPS_H "#ifdef $macro_from_ucs_ces\U$ces\n";
674     foreach my $dep (@deps)
675     {
676       print CESDEPS_H "#  ifndef $macro_from_ucs_ces\U$dep\n";
677       print CESDEPS_H "#    define $macro_from_ucs_ces\U$dep\n";
678       print CESDEPS_H "#  endif\n";
679     }
680     print CESDEPS_H "#endif\n";
681   }
682
683   print CESDEPS_H "\n#endif /* !__CESDEPS_H__ */\n\n";
684   close CESDEPS_H or err "Error while closing cesdeps.h file.";
685 }
686
687 # ==============================================================================
688 #
689 # Generate ccsbi.h file.
690 #
691 # Parameter 1 (input): hash reference with keys = CCS tables names and
692 # values = array references with list of encodings which need this CCS table.
693 #
694 # ==============================================================================
695 sub generate_ccsbi_h($)
696 {
697   my %ccsenc = %{$_[0]};
698   my @ccs = sort keys %ccsenc;
699   
700   print "Debug: create \"../ccs/ccsbi.h\" file.\n" if $verbose;
701   open (CCSBI_H, '>', "../ccs/ccsbi.h")
702   or err "Can't create \"../ccs/ccsbi.h\" file for writing.\nSystem error message: $!.\n";
703
704   print CCSBI_H "$comment_automatic\n\n";
705   print CCSBI_H "#ifndef __CCSBI_H__\n";
706   print CCSBI_H "#define __CCSBI_H__\n\n";
707   print CCSBI_H "#include <newlib.h>\n";
708   print CCSBI_H "#include <_ansi.h>\n";
709   print CCSBI_H "#include \"ccs.h\"\n\n";
710   print CCSBI_H "/*\n";
711   print CCSBI_H " * Enable CCS tables if encoding needs them.\n";
712   print CCSBI_H " * Defining ${macro_to_ucs_ccs}XXX macro or ${macro_from_ucs_ccs}XXX\n";
713   print CCSBI_H " * macro is needed to enable \"XXX encoding -> UCS\" or \"UCS -> XXX encoding\"\n";
714   print CCSBI_H " * part of CCS table.\n";
715   print CCSBI_H " * CCS tables aren't linked if Newlib was configuted to use external CCS tables.\n";
716   print CCSBI_H " */\n";
717   
718   print CCSBI_H "#ifndef _ICONV_ENABLE_EXTERNAL_CCS\n\n";
719
720   foreach my $ccs (@ccs)
721   {
722     my @encs = sort @{$ccsenc{$ccs}};
723     foreach my $encoding (@encs)
724     {
725       print CCSBI_H $encoding eq $encs[0] ? "#if " : " || ";
726       print CCSBI_H "defined ($macro_from_enc\U$encoding)";
727       print CCSBI_H " \\" if $encoding ne $encs[$#encs];
728       print CCSBI_H "\n";
729     }
730     print CCSBI_H "#  define $macro_to_ucs_ccs\U$ccs\n";
731     print CCSBI_H "#endif\n";
732     
733     foreach my $encoding (@encs)
734     {
735       print CCSBI_H $encoding eq $encs[0] ? "#if " : " || ";
736       print CCSBI_H "defined ($macro_to_enc\U$encoding)";
737       print CCSBI_H " \\" if $encoding ne $encs[$#encs];
738       print CCSBI_H "\n";
739     }
740     print CCSBI_H "#  define $macro_from_ucs_ccs\U$ccs\n";
741     print CCSBI_H "#endif\n\n";
742   }
743
744   print CCSBI_H "/*\n";
745   print CCSBI_H " * CCS table description structures forward declarations.\n";
746   print CCSBI_H " */\n";
747
748   foreach my $ccs (@ccs)
749   {
750     print CCSBI_H "#if defined ($macro_to_ucs_ccs\U$ccs) \\\n";
751     print CCSBI_H " || defined ($macro_from_ucs_ccs\U$ccs)\n";
752     print CCSBI_H "extern _CONST iconv_ccs_t\n";
753     print CCSBI_H "$var_ccs$ccs;\n";
754     print CCSBI_H "#endif\n";
755   }
756
757   print CCSBI_H "\n#endif /* !_ICONV_ENABLE_EXTERNAL_CCS */\n\n";
758   print CCSBI_H "\n#endif /* __CCSBI_H__ */\n\n";
759   close CCSBI_H or err "Error while closing ../ccs/ccsbi.h file.";
760 }
761
762 # ==============================================================================
763 #
764 # Generate cesbi.c file.
765 #
766 # Parameter 1 (input): hash reference with keys = CES Converters names and
767 # values = array references with list of supported encodings.
768 #
769 # ==============================================================================
770 sub generate_cesbi_c($)
771 {
772   my %cesenc = %{$_[0]};
773   my @ces = sort keys %cesenc;
774
775   print "Debug: create \"cesbi.c\" file.\n" if $verbose;
776   open (CESBI_C, '>', "cesbi.c")
777   or err "Can't create \"cesbi.c\" file for writing.\nSystem error message: $!.\n";
778
779   print CESBI_C "$comment_automatic\n\n";
780   print CESBI_C "#include <_ansi.h>\n";
781   print CESBI_C "#include <newlib.h>\n";
782   print CESBI_C "#include \"../lib/ucsconv.h\"\n";
783   print CESBI_C "#include \"cesbi.h\"\n\n";
784   print CESBI_C "/*\n";
785   print CESBI_C " * Each CES converter provides the list of supported encodings.\n";
786   print CESBI_C " */\n";
787
788   foreach my $ces (@ces)
789   {
790     print CESBI_C "#if defined ($macro_to_ucs_ces\U$ces) \\\n";
791     print CESBI_C " || defined ($macro_from_ucs_ces\U$ces)\n";
792     print CESBI_C "static _CONST char *\n";
793     print CESBI_C "$var_ces_names${ces}\[] =\n";
794     print CESBI_C "{\n";
795     my @encodings = sort @{$cesenc{$ces}};
796     foreach my $encoding (@encodings)
797     {
798       print CESBI_C "# if defined ($macro_from_enc\U$encoding) \\\n";
799       print CESBI_C "  || defined ($macro_to_enc\U$encoding)\n";
800       print CESBI_C "  $macro_enc_name\U$encoding,\n";
801       print CESBI_C "#endif\n";
802     }
803     print CESBI_C "  NULL\n";
804     print CESBI_C "};\n";
805     print CESBI_C "#endif\n\n";
806   }
807
808   print CESBI_C "/*\n";
809   print CESBI_C " * The following structure contains the list of \"to UCS\" linked-in CES converters.\n";
810   print CESBI_C " */\n";
811   print CESBI_C "_CONST iconv_to_ucs_ces_t\n";
812   print CESBI_C "_iconv_to_ucs_ces[] =\n";
813   print CESBI_C "{\n";
814   
815   foreach my $ces (@ces)
816   {
817     print CESBI_C "#ifdef $macro_to_ucs_ces\U$ces\n";
818     print CESBI_C "  {(_CONST char **)$var_ces_names$ces,\n";
819     print CESBI_C "   &$var_to_ucs_handlers$ces},\n";
820     print CESBI_C "#endif\n";
821   }
822   print CESBI_C "  {(_CONST char **)NULL,\n";
823   print CESBI_C "  (iconv_to_ucs_ces_handlers_t *)NULL}\n";
824   print CESBI_C "};\n\n";
825
826   print CESBI_C "/*\n";
827   print CESBI_C " * The following structure contains the list of \"from UCS\" linked-in CES converters.\n";
828   print CESBI_C " */\n";
829   print CESBI_C "_CONST iconv_from_ucs_ces_t\n";
830   print CESBI_C "_iconv_from_ucs_ces[] =\n";
831   print CESBI_C "{\n";
832   
833   foreach my $ces (@ces)
834   {
835     print CESBI_C "#ifdef $macro_from_ucs_ces\U$ces\n";
836     print CESBI_C "  {(_CONST char **)$var_ces_names$ces,\n";
837     print CESBI_C "   &$var_from_ucs_handlers$ces},\n";
838     print CESBI_C "#endif\n";
839   }
840   print CESBI_C "  {(_CONST char **)NULL,\n";
841   print CESBI_C "  (iconv_from_ucs_ces_handlers_t *)NULL}\n";
842   print CESBI_C "};\n";
843
844   close CESBI_C or err "Error while closing cesbi.c file.";
845 }
846
847 # ==============================================================================
848 #
849 # Generate ccsbi.c file.
850 #
851 # Parameter 1 (input): array reference with CCS tables names
852 #
853 # ==============================================================================
854 sub generate_ccsbi_c($)
855 {
856   my @ccs = @{$_[0]};
857   
858   print "Debug: create \"../ccs/ccsbi.c\" file.\n" if $verbose;
859   open (CESBI_C, '>', "../ccs/ccsbi.c")
860   or err "Can't create \"../ccs/ccsbi.c\" file for writing.\nSystem error message: $!.\n";
861
862   print CESBI_C "$comment_automatic\n\n";
863   print CESBI_C "#include <_ansi.h>\n";
864   print CESBI_C "#include \"ccsbi.h\"\n\n";
865   print CESBI_C "/*\n";
866   print CESBI_C " * The following array contains the list of built-in CCS tables.\n";
867   print CESBI_C " */\n";
868
869   print CESBI_C "_CONST iconv_ccs_t *\n";
870   print CESBI_C "_iconv_ccs[] =\n";
871   print CESBI_C "{\n";
872
873   foreach my $ccs (@ccs)
874   {
875     print CESBI_C "#if defined ($macro_to_ucs_ccs\U$ccs) \\\n";
876     print CESBI_C " || defined ($macro_from_ucs_ccs\U$ccs)\n";
877     print CESBI_C "  &$var_ccs$ccs,\n";
878     print CESBI_C "#endif\n";
879   }
880   print CESBI_C "  NULL\n";
881   print CESBI_C "};\n";
882
883   close CESBI_C or err "Error while closing ../ccs/ccsbi.c file.";
884 }
885
886 # ==============================================================================
887 #
888 # Generate ccsnames.h file.
889 #
890 # Parameter 1 (input): hash reference with keys = CCS tables names and
891 # values = array references with list of encodings which need this CCS table.
892 #
893 # ==============================================================================
894 sub generate_ccsnames_h($)
895 {
896   my %ccsenc = %{$_[0]};
897   my @ccs = sort keys %ccsenc;
898   
899   print "Debug: create \"../ccs/ccsnames.h\" file.\n" if $verbose;
900   open (CCSNAMES_H, '>', "../ccs/ccsnames.h")
901   or err "Can't create \"../ccs/ccsnames.h\" file for writing.\nSystem error message: $!.\n";
902
903   print CCSNAMES_H "$comment_automatic\n\n";
904   print CCSNAMES_H "#ifndef __CCSNAMES_H__\n";
905   print CCSNAMES_H "#define __CCSNAMES_H__\n\n";
906   print CCSNAMES_H "#include \"../lib/encnames.h\"\n\n";
907   print CCSNAMES_H "/*\n";
908   print CCSNAMES_H " * CCS tables names macros.\n";
909   print CCSNAMES_H " */\n";
910
911   foreach my $ccs (@ccs)
912   {
913     my @encs = @{$ccsenc{$ccs}};
914     my $flag;
915     foreach my $encoding (@encs)
916     {
917       print CCSNAMES_H "#define $macro_ccs_name\U$ccs ";
918       if ($encoding eq $ccs)
919       {
920         $flag = 1;
921         print CCSNAMES_H "$macro_enc_name\U$encoding\n";
922         last;
923       }
924     }
925     print CCSNAMES_H "\"$ccs\"\n" if !$flag;
926   }
927
928   print CCSNAMES_H "\n#endif /* !__CCSNAMES_H__ */\n\n";
929   close CCSNAMES_H or err "Error while closing ../ccs/ccsnames.h file.";
930 }