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#
26use integer;
27use Getopt::Std;
28use IO::Seekable;
29use strict;
30
31
32# ##############################################################################
33#
34# FUNCTION PROTOTYPES AND GLOBAL DATA DECLARATION SECTION
35#
36# ##############################################################################
37
38
39# SUPPLEMENTARY FUNCTIONS FORWARD DECLARATIONS
40sub ProcessOptions();
41sub Err($;$);
42sub Generate8bitToUCS();
43sub GenerateSpeed($);
44sub Generate16bitSize($);
45sub Output8bitToUCS(;$);
46sub Output8bitFromUCS(;$);
47sub OutputSpeed($;$);
48sub OutputSize($;$);
49
50# VARIABLES RELATING TO COMMAND-LINE OPTIONS
51my $Verbose;  # Be verbose if true
52my $Source;   # Output C source code instead of binary .cct file if true
53my $Plane;    # Use this plane if defined
54my $InFile;   # Use this file for input
55my $OutFile;  # Use this file for output
56my $CCSName;  # Use this CCS name
57my $NoSpeed;  # Don't generate speed-optimized tables (binary files only)
58my $NoSize;   # Don't generate size-optimized tables (binary files only)
59my $NoBE;     # Don't generate big-endian tables (binary files only)
60my $NoLE;     # Don't generate big-endian tables (binary files only)
61my $NoTo;     # Don't generate "to_ucs" table (binary files only)
62my $NoFrom;   # Don't generate "from_ucs" table (binary files only)
63my $CCSCol;   # CCS column number in source file
64my $UCSCol;   # UCS column number in source file
65
66
67# DATA STRUCTURES WITH "TO_UCS" AND "FROM_UCS" SPEED/SIZE -OPTIMIZED TABLES
68my (@FromSpeedTbl, @ToSpeedTbl, @FromSizeTbl, @ToSizeTbl);
69# "TO_UCS" AND "FROM_UCS" SPEED/SIZE -OPTIMIZED TABLES SIZE IN BYTES
70my ($FromSpeedBytes, $ToSpeedBytes, $FromSizeBytes, $ToSizeBytes) =
71   (0, 0, 0, 0);
72
73my (%CCSUCS, %UCSCCS); # CCS->UCS and UCS->CCS mappings
74my $Bits = 8;          # Table bits (8 or 16);
75
76# SPECIAL MARKER CODES
77my $InvCode  = 0xFFFF; # FFFF indicates 18 bit invalid codes
78my $InvBlock = 0xFFFF; # FFFF also mark empty blocks in speed-optimized tables
79my $LostCode = 0x3F;   # ASCII '?' marks codes lost during CCS->UCS mapping
80# To mark invalid codes in 8bit encodings 0xFF is used CCS's 0xFF mapping is saved
81# separately. $FFMap variable stores real 0xFF mapping if defined.
82my $InvCode8bit = 0xFF;
83my $FFMap;
84
85# 8 Bit "From UCS" table header size (bytes)
86my $Hdr8bitFromUCS = 2;
87# Binary table header size (bytes)
88my $HdrBinary = 8;
89
90# At first all lost CCS codes are marked by $TmpLost to distinguish between
91# code which is equivalent to $LostCode and lost codes. This is done in order to
92# output $MacroLostCode instead of $LostCode in source file.
93my $TmpLost = 0x1FFFF;
94
95# VARIABLES RELATING TO C SOURCE CODE
96my $MacroInvCode      = 'INVALC';
97my $MacroInvBlock     = 'INVBLK';
98my $MacroLostCode     = 'LOST_C';
99my $MacroCCSName      = 'ICONV_CCS_%s';
100my $GuardSize       = 'defined (TABLE_USE_SIZE_OPTIMIZATION)';
101my $GuardToUCS      = "ICONV_TO_UCS_CCS_%s";
102my $GuardFromUCS    = "ICONV_FROM_UCS_CCS_%s";
103my $MacroSpeedTable = 'TABLE_SPEED_OPTIMIZED';
104my $MacroSizeTable  = 'TABLE_SIZE_OPTIMIZED';
105my $Macro8bitTable  = 'TABLE_8BIT';
106my $Macro16bitTable = 'TABLE_16BIT';
107my $MacroVer1Table  = 'TABLE_VERSION_1';
108my $TypeBICCS       = 'iconv_ccs_t';
109my $VarToUCSSize    = "to_ucs_size_%s";
110my $VarToUCSSpeed   = "to_ucs_speed_%s";
111my $VarFromUCSSize  = "from_ucs_size_%s";
112my $VarFromUCSSpeed = "from_ucs_speed_%s";
113my $VarBICCS             = "_iconv_ccs_%s";
114# Text block that visually separates tables.
115my $Separator = '=' x 70;
116
117# ##############################################################################
118#
119# SCRIPT ENTRY POINT
120#
121# ##############################################################################
122
123
124# Parse command-line options, check them and set correspondent global variables
125ProcessOptions();
126
127# Initialize global variables tat depend on CCS name.
128$_ = sprintf $_, $CCSName foreach +($VarToUCSSpeed,
129                                    $VarToUCSSize,
130                                    $VarToUCSSpeed,
131                                    $VarFromUCSSpeed,
132                                    $VarFromUCSSize,
133                                    $VarBICCS);
134$_ = sprintf $_, "\U$CCSName" foreach +($GuardToUCS,
135                                        $GuardFromUCS,
136                                        $MacroCCSName);
137
138# Open input and output files
139Err "Can't open \"$InFile\" file for reading: $!.\n", 1
140unless open(INFILE, '<', $InFile);
141Err "Can't open \"$OutFile\" file for writing: $!.\n", 1
142unless open(OUTFILE, '>', $OutFile);
143
144# ==============================================================================
145# EXTRACT CODES MAP FROM INPUT FILE
146# ==============================================================================
147
148for (my $ln = 1; my $l = <INFILE>; $ln += 1)
149{
150  # Skip comment and empty lines, remove ending CR symbol
151  next if $l =~ /^#.*$/ or $l =~ /^\s*$/;
152  $l =~ s/^(.*)\n$/$1/, $l =~ s/^(.*)\r$/$1/;
153
154  # Remove comment and extra spaces
155  $l =~ s/(.*)\s*#.*/$1/;
156  $l =~ s/\s+/ /g;
157  $l =~ s/(.*)\s*$/$1/;
158
159  # Split line into individual codes
160  my @codes = split / /, $l;
161
162  # Skip line if there is no needed columns
163  unless (defined $codes[$CCSCol])
164  {
165    print("Warning (line $ln): no CCS column, skip.\n") if $Verbose;
166    next;
167  }
168  unless (defined $codes[$UCSCol])
169  {
170    print("Warning (line $ln): no UCS column, skip.\n") if $Verbose;
171    next;
172  }
173
174  # Extract codes strings from needed columns
175  my ($ccs, $ucs) = ($codes[$CCSCol], $codes[$UCSCol]);
176  my $patt = qr/(0[xX])?[0-9a-fA-F]{1,8}/; # HEX digit regexp pattern.
177
178  # Check that CCS and UCS code strings has right format.
179  unless ($ccs =~ m/^$patt$/)
180  {
181    print("Warning (line $ln): $ccs CCS code isn't recognized, skip.\n")
182    if $Verbose;
183    next;
184  }
185  unless ($ucs =~ m/^($patt(,|\+))*$patt$/)
186  {
187    print("Warning (line $ln): $ucs UCS code isn't recognized, skip.\n")
188    if $Verbose;
189    next;
190  }
191
192  # Convert code to numeric format (assume hex).
193  $ccs = hex $ccs;
194
195  if ($ucs =~ m/,/ or $ucs =~ m/\+/)
196  {
197    # Mark CCS codes with "one to many" mappings as lost
198    printf "Warning (line $ln): only one to one mapping is supported, "
199         . "mark 0x%.4X CCS code as lost.\n", hex $ccs if $Verbose;
200    $ucs = $TmpLost;
201  }
202  else
203  {
204    # Convert code to numeric format
205    $ucs = hex $ucs;
206
207    # Check that UCS code isn't longer than 16 bits.
208    if ($ucs > 0xFFFF)
209    {
210      printf("Warning (line $ln): UCS code should fit 16 bits, "
211           . "mark 0x%.4X CCS code as lost.\n", hex $ccs) if $Verbose;
212      $ucs = $TmpLost;
213    }
214  }
215
216  # If CCS value > 0xFFFF user should specify plane number.
217  if ($ccs > 0xFFFF && !defined $Plane)
218  {
219    print("Warning (line $ln): $ccs is > 16 bit, plane number should be specified,"
220        . " skip this mapping.\n") if $Verbose;
221    next;
222  }
223
224  if (defined $Plane)
225  {
226    next if (($ccs & 0xFFFF0000) >> 16) != hex $Plane; # Skip alien plane.
227    $ccs &= 0xFFFF;
228  }
229
230  # Check that reserved codes aren't used.
231  if ($ccs == $InvCode or $ucs == $InvCode)
232  {
233    print("Warning (line $ln): $InvCode is reserved to mark invalid codes and "
234       . "shouldn't be used in mappings, skip.\n") if $Verbose;
235    next;
236  }
237
238  # Save mapping in UCSCCS and CCSUCS hash arrays.
239  $UCSCCS{$ucs} = $ccs if $ucs != $TmpLost && !defined $UCSCCS{$ucs};
240  $CCSUCS{$ccs} = $ucs if !defined $CCSUCS{$ccs};
241
242  $Bits = 16 if $ccs > 0xFF;
243}
244
245if (not %CCSUCS)
246{
247  Err "Error: there is no plane $Plane in \"$0\".\n" if defined $Plane;
248  Err "Error: mapping wasn't found.\n";
249}
250
251
252# ==============================================================================
253# GENERATE TABLE DATA
254# ==============================================================================
255
256if ($Bits == 8)
257{
258  $FFMap = $CCSUCS{0xFF};
259  $FFMap = $InvCode if !defined $FFMap;
260}
261
262if ($Bits == 8)
263{
264  Generate8bitToUCS() unless $NoTo;
265}
266else
267{
268  GenerateSpeed("to_ucs") unless $NoTo || $NoSpeed;
269  Generate16bitSize("to_ucs")  unless $NoTo || $NoSize;
270}
271
272GenerateSpeed("from_ucs") unless $NoFrom || $NoSpeed;
273Generate16bitSize("from_ucs")  unless $NoFrom || $NoSize;
274
275# ==============================================================================
276# OUTPUT ARRAYS
277# ==============================================================================
278
279if ($Source)
280{
281  # OUTPUT SOURCE
282  print OUTFILE
283"/*
284 * This file was generated automatically - don't edit it.
285 * File contains iconv CCS tables for $CCSName encoding.
286 */
287
288#include \"ccsbi.h\"
289
290#if defined ($GuardToUCS) \\
291 || defined ($GuardFromUCS)
292
293#include <sys/types.h>
294#include <sys/param.h>
295#include \"ccs.h\"
296#include \"ccsnames.h\"
297
298";
299
300  if ($Bits == 8)
301  {
302    print OUTFILE
303"#if (_BYTE_ORDER == _LITTLE_ENDIAN)
304#  define W(word) (word) & 0xFF, (word) >> 8
305#elif (_BYTE_ORDER == _BIG_ENDIAN)
306#  define W(word) (word) >> 8, (word) & 0xFF
307#else
308#  error \"Unknown byte order.\"
309#endif
310
311";
312  }
313
314  unless ($NoTo)
315  {
316    if ($Bits == 8)
317    {
318      Output8bitToUCS();
319    }
320    else
321    {
322      OutputSpeed("to_ucs") unless $NoSpeed;
323      OutputSize("to_ucs")  unless $NoSize;
324    }
325  }
326  unless ($NoFrom)
327  {
328    if ($Bits == 8)
329    {
330      Output8bitFromUCS();
331    }
332    else
333    {
334      OutputSpeed("from_ucs") unless $NoSpeed;
335      OutputSize("from_ucs")  unless $NoSize;
336    }
337  }
338
339  # OUTPUT TABLE DESCRIPTION STRUCTURE
340  print OUTFILE
341"/*
342 * $CCSName CCS description table.
343 * $Separator
344 */
345const $TypeBICCS
346$VarBICCS =
347{
348\t$MacroVer1Table, /* Table version */
349\t$MacroCCSName, /* CCS name */
350";
351  if ($Bits == 8)
352  {
353    print OUTFILE
354"\t$Macro8bitTable, /* Table bits */
355\t0, /* Not Used */
356#if defined ($GuardFromUCS)
357\t(__uint16_t *)&$VarFromUCSSpeed, /* UCS -> $CCSName table */
358#else
359\t(__uint16_t *)NULL,
360#endif
361\t0, /* Not Used */
362#if defined ($GuardToUCS)
363\t(__uint16_t *)&$VarToUCSSpeed /* $CCSName -> UCS table */
364#else
365\t(__uint16_t *)NULL,
366#endif
367};\n";
368  }
369  else
370  {
371    print OUTFILE
372"\t$Macro16bitTable, /* Table bits */
373#if defined ($GuardFromUCS) \\
374 && ($GuardSize)
375\t$MacroSizeTable,
376\t(__uint16_t *)&$VarFromUCSSize, /* UCS -> $CCSName table size-optimized table */
377#elif defined ($GuardFromUCS) \\
378 && !($GuardSize)
379\t$MacroSpeedTable,
380\t(__uint16_t *)&$VarFromUCSSpeed, /* UCS -> $CCSName table speed-optimized table */
381#else
382\t$MacroSpeedTable,
383\t(__uint16_t *)NULL,
384#endif
385#if defined ($GuardToUCS) \\
386 && ($GuardSize)
387\t$MacroSizeTable,
388\t(__uint16_t *)&$VarToUCSSize /* $CCSName -> UCS table speed-optimized table */
389#elif defined ($GuardToUCS) \\
390 && !($GuardSize)
391\t$MacroSpeedTable,
392\t(__uint16_t *)&$VarToUCSSpeed /* $CCSName -> UCS table speed-optimized table */
393#else
394\t$MacroSpeedTable,
395\t(__uint16_t *)NULL,
396#endif
397};\n";
398  }
399  print OUTFILE "\n#endif /* $GuardToUCS) || ... */\n\n";
400}
401else
402{
403  # OUTPUT BINARY TABLES DESCRIPTION STRUCTURE (ALWAYS BIG ENDIAN)
404  print OUTFILE pack "n", 1;
405  print OUTFILE pack "n", $Bits;
406  my $len = length $CCSName;
407  print OUTFILE pack "N", $len;
408  print OUTFILE pack "a$len", $CCSName;
409
410  my $pos = $HdrBinary + $len;
411  if ($pos & 3)
412  {
413    my $l = 4 - ($pos & 3);
414    print OUTFILE pack "a$l", 'XXX';
415    $pos += $l;
416  }
417
418  $pos += 16*4;
419
420  my @tables;
421  for (my $i = 0; $i < 16; $i++)
422  {
423    $tables[$i] = 0;
424  }
425
426  $tables[0] = $pos, $tables[1] = $FromSpeedBytes, $pos += $FromSpeedBytes
427  unless $NoFrom || $NoSpeed || $NoBE;
428  $tables[2] = $pos, $tables[3] = $FromSpeedBytes, $pos += $FromSpeedBytes
429  unless $NoFrom || $NoSpeed || $NoLE;
430  if ($Bits == 16)
431  {
432    $tables[4] = $pos, $tables[5] = $FromSizeBytes, $pos += $FromSizeBytes
433    unless $NoFrom || $NoSize || $NoBE;
434    $tables[6] = $pos, $tables[7] = $FromSizeBytes, $pos += $FromSizeBytes
435    unless $NoFrom || $NoSize || $NoLE;
436  }
437  $tables[8] = $pos, $tables[9] = $ToSpeedBytes, $pos += $ToSpeedBytes
438  unless $NoTo || $NoSpeed || $NoBE;
439  $tables[10] = $pos, $tables[11] = $ToSpeedBytes, $pos += $ToSpeedBytes
440  unless $NoTo || $NoSpeed || $NoLE;
441  if ($Bits == 16)
442  {
443    $tables[12] = $pos, $tables[13] = $ToSizeBytes, $pos += $ToSizeBytes
444    unless $NoTo || $NoSize || $NoBE;
445    $tables[14] = $pos, $tables[15] = $ToSizeBytes, $pos += $ToSizeBytes
446    unless $NoTo || $NoSize || $NoLE;
447  }
448
449  print OUTFILE pack("N", $_) foreach @tables;
450
451  print "Total bytes for output: $pos.\n" if $Verbose;
452
453  # OUTPUT BINARY TABLES
454  unless ($NoFrom)
455  {
456    if ($Bits == 8)
457    {
458      Output8bitFromUCS("n") unless $NoBE;
459      Output8bitFromUCS("v") unless $NoLE;
460    }
461    else
462    {
463      unless ($NoSpeed)
464      {
465        OutputSpeed("from_ucs", "n") unless $NoBE;
466        OutputSpeed("from_ucs", "v") unless $NoLE;
467      }
468      unless ($NoSize)
469      {
470        OutputSize("from_ucs", "n") unless $NoBE;
471        OutputSize("from_ucs", "v") unless $NoLE;
472      }
473    }
474  }
475  unless ($NoTo)
476  {
477    if ($Bits == 8)
478    {
479      Output8bitToUCS("n") unless $NoBE;
480      Output8bitToUCS("v") unless $NoLE;
481    }
482    else
483    {
484      unless ($NoSpeed)
485      {
486        OutputSpeed("to_ucs", "n") unless $NoBE;
487        OutputSpeed("to_ucs", "v") unless $NoLE;
488      }
489      unless ($NoSize)
490      {
491        OutputSize("to_ucs", "n") unless $NoBE;
492        OutputSize("to_ucs", "v") unless $NoLE;
493      }
494    }
495  }
496}
497
498close INFILE;
499close OUTFILE;
500exit 0;
501
502
503# ##############################################################################
504#
505# SUPPLEMENTARY FUNCTIONS
506#
507# ##############################################################################
508
509
510# =============================================================================
511#
512# Generate 8bit "to_ucs" table. Store table data in %ToSpeedTbl hash.
513# Store table size in $ToSpeedBytes scalar.
514#
515# =============================================================================
516sub Generate8bitToUCS()
517{
518  for (my $i = 0; $i <= 255; $i++)
519  {
520    $ToSpeedTbl[$i] = defined $CCSUCS{$i} ? $CCSUCS{$i} : $InvCode;
521  }
522  $ToSpeedBytes = 256*2;
523}
524
525
526# =============================================================================
527#
528# Generate speed-optimized table.
529#
530# Parameter 1:
531#    "to_ucs"   - generate "to_ucs" table, store table data in @ToSpeedTbl
532#                 array, store table size in $ToSpeedBytes scalar.
533#    "from_ucs" - generate "from_ucs" table, store table data in @FromSpeedTbl
534#                 array, store table size in $FromSpeedBytes scalar.
535#
536# Data is written to @ToSpeedTbl or @FromSpeedTbl (@map) table and has the
537# following format:
538# $table[0] - 256-element array (control block);
539# $table[1 .. $#table] - 256-element arrays (data blocks).
540#
541# =============================================================================
542sub GenerateSpeed($)
543{
544  my $map;
545  my $tbl;
546  my $bytes;
547
548  if ($_[0] eq "to_ucs")
549  {
550    $map = \%CCSUCS;
551    $tbl = \@ToSpeedTbl;
552    $bytes = \$ToSpeedBytes;
553  }
554  elsif ($_[0] eq "from_ucs")
555  {
556    $map = \%UCSCCS;
557    $tbl = \@FromSpeedTbl;
558    $bytes = \$FromSpeedBytes;
559  }
560  else
561  {
562    Err "Internal script error in GenerateSpeed()\n";
563  }
564
565  # Identify unused blocks
566  my @busy_blocks;
567  $busy_blocks[$_ >> 8] = 1 foreach (keys %$map);
568
569  # GENERATE FIRST 256-ELEMENT CONTROL BLOCK
570  for (my $i = 0,
571       my $idx = $Bits == 16 ? 0 : 256 + $Hdr8bitFromUCS;
572       $i <= 0xFF; $i++)
573  {
574    $tbl->[0]->[$i] = $busy_blocks[$i] ? $idx += 256 : undef;
575  }
576
577  # GENERATE DATA BLOCKS
578  $$bytes = 0;
579  for (my $i = 0; $i <= 0xFF; $i++)
580  {
581    next unless $busy_blocks[$i];
582    $$bytes += 256;
583    for (my $j = 0; $j <= 0xFF; $j++)
584    {
585      $tbl->[$i+1]->[$j] = $map->{($i << 8) | $j};
586    }
587  }
588  $$bytes *= 2 if $Bits == 16;
589  $$bytes += $Hdr8bitFromUCS if $Bits == 8;
590  $$bytes += 512;
591}
592
593
594# =============================================================================
595#
596# Generate 16bit size-optimized table.
597#
598# Parameter 1:
599#    "to_ucs"   - generate "to_ucs" table, store table data in @ToSizeTbl
600#                 array, store table size in $ToSizeBytes scalar.
601#    "from_ucs" - generate "from_ucs" table, store table data in @FromSizeTbl
602#                 array, store table size in $FromSizeBytes scalar.
603#
604# Data is written to @ToSizeTbl or @FromSizeTbl (@map) table and has the
605# following format:
606# $table[0] - number of ranges;
607# $table[1] - number of unranged codes;
608# $table[2] - unranged codes index in resulting array;
609# $table[3]->[0 .. $table[0]] - array of arrays of ranges:
610#     $table[3]->[x]->[0] - first code;
611#     $table[3]->[x]->[1] - last code;
612#     $table[3]->[x]->[2] - range index in resulting array;
613# $table[4]->[0 .. $table[0]] - array of arrays of ranges content;
614# $table[5]->[0 .. $table[1]] - array of arrays of unranged codes;
615#     $table[5]->[x]->[0] - CCS code;
616#     $table[5]->[x]->[0] - UCS code;
617#
618# =============================================================================
619sub Generate16bitSize($)
620{
621  my $map;
622  my $tbl;
623  my $bytes;
624
625  if ($_[0] eq "to_ucs")
626  {
627    $map = \%CCSUCS;
628    $tbl = \@ToSizeTbl;
629    $bytes = \$ToSizeBytes;
630  }
631  elsif ($_[0] eq "from_ucs")
632  {
633    $map = \%UCSCCS;
634    $tbl = \@FromSizeTbl;
635    $bytes = \$FromSizeBytes;
636  }
637  else
638  {
639    Err "Internal script error  Generate16bitSize()\n";
640  }
641
642  # CREATE LIST OF RANGES.
643  my @codes = sort {$a <=> $b} keys %$map;
644  my @ranges;  # Code ranges
645  my @range;   # Current working range
646  foreach (@codes)
647  {
648    if (not @range or $_ - 1 == $range[$#range])
649    {
650      push @range, $_;
651    }
652    else
653    {
654      my @tmp = @range;
655      push @ranges, \@tmp;
656      undef @range;
657      redo;
658    }
659  }
660  # Add Last range too
661  if (@range)
662  {
663    my @tmp = @range;
664    push @ranges, \@tmp;
665  }
666
667  # OPTIMIZE LIST OF RANGES.
668  my $r = 0; # Working range number
669  while (1)
670  {
671    last if ($r == $#ranges);
672
673    my @r1 = @{$ranges[$r]};
674    my @r2 = @{$ranges[$r + 1]};
675
676    # Calculate how many array entries two ranges need
677    my ($s1, $s2);
678
679    if ($#r1 == 0)
680    { $s1 = 2; }
681    elsif ($#r1 == 1)
682    { $s1 = 4; }
683    else
684    { $s1 = $#r1 + 1 + 3; }
685
686    if ($#r2 == 0)
687    { $s2 = 2; }
688    elsif ($#r2 == 1)
689    { $s2 = 4; }
690    else
691    { $s2 = $#r2 + 1 + 3; }
692
693    my $two = $s1 + $s2;
694
695    # Calculate how many array entries will be needed if we join them
696    my $one = $r2[$#r2] - $r1[0] + 1 + 3;
697
698    $r += 1, next if ($one > $two);
699
700    # Join ranges
701    my @r; # New range.
702    push @r, $_ foreach (@r1);
703    for (my $i = $r1[$#r1]+1; $i < $r2[0]; $i++)
704    {
705      push @r, undef;
706    }
707    push @r, $_ foreach (@r2);
708    $ranges[$r] = \@r;
709    splice @ranges, $r+1, 1;
710  }
711
712  # SEPARATE RANGED AND UNRANGED CODES. SPLIT 2-CODES RANGES ON 2 UNRANGED.
713  my @unranged;
714  foreach (@ranges)
715  {
716    if ($#$_ == 0)
717    {
718      push @unranged, $$_[0];
719      undef $_;
720    }
721    elsif ($#$_ == 1)
722    {
723      push @unranged, $$_[0];
724      push @unranged, $$_[1];
725      undef $_;
726    }
727  }
728
729  # DELETE UNUSED ELEMENTS
730  for (my $i = 0; $i <= $#ranges; $i++)
731  {
732    splice @ranges, $i--, 1 unless defined $ranges[$i];
733  }
734
735  # CALCULATE UNRANGED CODES ARRAY INDEX
736  my $idx = 3 + ($#ranges + 1)*3;
737  $idx += $#$_ + 1 foreach @ranges;
738
739  # COMPOSE TABLE
740  $tbl->[0] = $#ranges + 1;   # Number of ranges
741  $tbl->[1] = $#unranged + 1; # Number of unranged codes
742  $tbl->[2] = $idx;           # Array index of unranged codes
743
744  # Generate ranges list
745  $idx = 3 + ($#ranges + 1)*3; # First range data index
746  $$bytes = $idx*2;
747  my $num = 0;
748  foreach (@ranges)
749  {
750    $tbl->[3]->[$num]->[0] = $_->[0];
751    $tbl->[3]->[$num]->[1] = $_->[$#$_];
752    $tbl->[3]->[$num]->[2] = $idx;
753    $idx += $#$_ + 1;
754    $num += 1;
755  }
756
757  # Generate ranges content
758  $num = 0;
759  foreach (@ranges)
760  {
761    for (my $i = 0; $i <= $#$_; $i++)
762    {
763      $tbl->[4]->[$num]->[$i] = defined $_->[$i] ? $map->{$_->[$i]} : undef;
764    }
765    $num += 1;
766    $$bytes += ($#$_ + 1)*2;
767  }
768
769  # Generate unranged codes list
770  $num = 0;
771  foreach (@unranged)
772  {
773    $tbl->[5]->[$num]->[0] = $_;
774    $tbl->[5]->[$num]->[1] = $map->{$_};
775    $num += 1;
776  }
777
778  $$bytes += ($#unranged + 1)*4;
779}
780
781
782# =============================================================================
783#
784# Output 8bit "to UCS" table. Output table's source code if $Source
785# and table's binary data if !$Source.
786#
787# Parameter 1: Not used when sources are output. Output BE binary if 'n' and
788#              LE binary if 'v'.
789#
790# =============================================================================
791sub Output8bitToUCS(;$)
792{
793  my $endian = $_[0];
794  my $br = 0;
795
796  printf "Output%s 8-bit UCS -> $CCSName table ($ToSpeedBytes bytes).\n",
797         defined $endian ? ($endian eq 'n' ?
798                 " Big Endian" : " Little Endian") : "" if $Verbose;
799  if ($Source)
800  {
801    # Output heading information
802    printf OUTFILE
803"/*
804 * 8-bit $CCSName -> UCS table ($ToSpeedBytes bytes).
805 * $Separator
806 */
807#if defined ($GuardToUCS)
808
809static const __uint16_t
810${VarToUCSSpeed}\[] =
811{\n\t";
812  }
813
814  if ($Source)
815  {
816    foreach (@ToSpeedTbl)
817    {
818      $br += 1;
819      if ($_ != $InvCode)
820      {
821        if ($_ != $TmpLost)
822        {
823          printf OUTFILE "0x%.4X,", $_;
824        }
825        else
826        {
827          print OUTFILE "$MacroLostCode,";
828        }
829      }
830      else
831      {
832        print OUTFILE "$MacroInvCode,";
833      }
834      print(OUTFILE "\n\t"), $br = 0 unless $br % 8;
835    }
836    print OUTFILE "\n};\n\n#endif /* $GuardToUCS */\n\n";
837  }
838  else
839  {
840    foreach (@ToSpeedTbl)
841    {
842      print OUTFILE pack($endian, $_ == $TmpLost ? $LostCode : $_);
843    }
844  }
845}
846
847
848# =============================================================================
849#
850# Output 8bit "from UCS" table. Output table's source code if $Source
851# and table's binary data if !$Source.
852#
853# Parameter 1: Not used when sources are output. Output BE binary if 'n' and
854#              LE binary if 'v'.
855#
856# =============================================================================
857sub Output8bitFromUCS(;$)
858{
859  my $endian = $_[0];
860
861  printf "Output%s 8-bit $CCSName -> UCS table ($FromSpeedBytes bytes).\n",
862         defined $endian ? ($endian eq 'n' ?
863                 " Big Endian" : " Little Endian") : "" if $Verbose;
864  if ($Source)
865  {
866    print OUTFILE
867"/*
868 * 8-bit UCS -> $CCSName speed-optimized table ($FromSpeedBytes bytes).
869 * $Separator
870 */
871
872#if defined ($GuardFromUCS)
873
874static const unsigned char
875${VarFromUCSSpeed}\[] =
876{
877";
878  }
879
880  # SAVE 0xFF MAPPING.
881  if ($Source)
882  {
883    printf OUTFILE "\tW(0x%.4X), /* Real 0xFF mapping. 0xFF is used "
884                 . "to mark invalid codes */\n", $FFMap;
885  }
886  else
887  {
888    print OUTFILE pack($endian, $FFMap);
889  }
890
891  # OUTPUT HEADING BLOCK (ALWAYS 16 BIT)
892  if ($Source)
893  {
894    my $count = 0;
895    print OUTFILE "\t/* Heading Block */";
896    for (my $i = 0, my $br = 0; $i < 256; $br = ++$i % 4)
897    {
898      print OUTFILE "\n\t" unless $br;
899      if (defined $FromSpeedTbl[0]->[$i])
900      {
901        printf OUTFILE "W(0x%.4X),", $FromSpeedTbl[0]->[$i];
902      }
903      else
904      {
905        print OUTFILE "W($MacroInvBlock),";
906      }
907    }
908  }
909  else
910  {
911    print OUTFILE pack($endian, defined $_ ? $_ : $InvBlock)
912    foreach @{$FromSpeedTbl[0]};
913  }
914
915  if ($Source)
916  {
917    my $index = 512 + $Hdr8bitFromUCS;
918    for (my $blk = 1; $blk <= $#FromSpeedTbl; $blk++)
919    {
920      next unless defined $FromSpeedTbl[$blk];
921      printf OUTFILE "\n\t/* Block $blk, Array index 0x%.4X */", $index;
922      $index += 256;
923      for (my $i = 0, my $br = 0; $i < 256; $i++, $br = $i % 8)
924      {
925        print OUTFILE "\n\t" unless $br;
926        my $code = $FromSpeedTbl[$blk]->[$i];
927        if (!defined $code)
928        {
929          printf OUTFILE "0x%.2X,", $InvCode8bit;
930        }
931        else
932        {
933          printf OUTFILE "0x%.2X,", $code == $TmpLost ? $LostCode : $code;
934        }
935      }
936    }
937    print OUTFILE "\n};\n\n#endif /* $GuardFromUCS */\n\n";
938  }
939  else
940  {
941    for (my $blk = 1; $blk <= $#FromSpeedTbl; $blk++)
942    {
943      next unless defined $FromSpeedTbl[$blk];
944      for (my $i = 0, my $br = 0; $i < 256; $br = ++$i % 8)
945      {
946        my $code = $FromSpeedTbl[$blk]->[$i];
947        if (!defined $code)
948        {
949          printf OUTFILE pack 'C', $InvCode8bit;
950        }
951        else
952        {
953          print OUTFILE $code == $TmpLost ? pack('C', $LostCode)
954                                          : pack('C', $code);
955        }
956      }
957    }
958  }
959}
960
961
962# =============================================================================
963#
964# Output 16bit Speed-optimized table. Output table's source code if $Source
965# and table's binary data if !$Source.
966#
967# Parameter 1:
968#    "to_ucs"   - Output "to_ucs" table.
969#    "from_ucs" - Output "from_ucs" table.
970# Parameter 2:    Not used when sources are output. Output BE binary if 'n' and
971#                 LE binary if 'v'.
972#
973# =============================================================================
974sub OutputSpeed($;$)
975{
976  my $endian = $_[1];
977  my $tbl;
978  my ($direction, $optimiz, $e, $bytes);
979  $optimiz = $Bits == 16 ? " speed-optimized" : "";
980  $e = $endian ? ($endian eq 'n' ? " Big Endian" : " Little Endian") : "";
981  if ($_[0] eq "to_ucs")
982  {
983    $tbl = \@ToSpeedTbl;
984    $direction = " $CCSName -> UCS";
985    $bytes = $ToSpeedBytes;
986
987    if ($Source)
988    {
989      print OUTFILE
990"/*
991 * 16-bit $CCSName -> UCS speed-optimized table ($ToSpeedBytes bytes).
992 * $Separator
993 */
994#if defined ($GuardToUCS) \\
995 && !($GuardSize)
996
997static const __uint16_t
998${VarToUCSSpeed}\[] =
999{
1000";
1001    }
1002  }
1003  elsif ($_[0] eq "from_ucs")
1004  {
1005    $tbl = \@FromSpeedTbl;
1006    $direction = " UCS -> $CCSName";
1007    $bytes = $FromSpeedBytes;
1008
1009    if ($Source)
1010    {
1011      print OUTFILE
1012"/*
1013 * 16-bit UCS -> $CCSName speed-optimized table ($FromSpeedBytes bytes).
1014 * $Separator
1015 */
1016
1017#if defined ($GuardFromUCS) \\
1018 && !($GuardSize)
1019
1020static const __uint16_t
1021${VarFromUCSSpeed}\[] =
1022{
1023";
1024    }
1025  }
1026  else
1027  {
1028    Err "Internal script error  Output16bitSpeed()\n";
1029  }
1030
1031  printf "Output%s 16-bit%s%s table (%d bytes).\n",
1032  $e, $direction, $optimiz, $bytes if $Verbose;
1033
1034  # OUTPUT HEADING BLOCK (ALWAYS 16 BIT)
1035  if ($Source)
1036  {
1037    my $count = 0;
1038    print OUTFILE "\t/* Heading Block */";
1039    for (my $i = 0, my $br = 0; $i < 256; $br = ++$i % 8)
1040    {
1041      print OUTFILE "\n\t" unless $br;
1042      if (defined $tbl->[0]->[$i])
1043      {
1044        printf OUTFILE "0x%.4X,", $tbl->[0]->[$i];
1045      }
1046      else
1047      {
1048        print OUTFILE "$MacroInvBlock,";
1049      }
1050    }
1051  }
1052  else
1053  {
1054    print OUTFILE pack($endian, defined $_ ? $_ : $InvBlock)
1055    foreach @{$tbl->[0]};
1056  }
1057
1058  # OUTPUT OTHER BLOCKS
1059  if ($Source)
1060  {
1061    my $index = 256;
1062    for (my $blk = 1; $blk <= $#$tbl; $blk++)
1063    {
1064      next unless defined $tbl->[$blk];
1065      printf OUTFILE "\n\t/* Block $blk, Array index 0x%.4X */", $index;
1066      $index += 256;
1067      for (my $i = 0, my $br = 0; $i < 256; $br = ++$i % 8)
1068      {
1069        print OUTFILE "\n\t" unless $br;
1070        my $code = $tbl->[$blk]->[$i];
1071        print OUTFILE defined $code ?
1072            ($code == $TmpLost ? $MacroLostCode : sprintf "0x%.4X", $code)
1073                                     : $MacroInvCode, ",";
1074      }
1075    }
1076  }
1077  else
1078  {
1079    for (my $blk = 1; $blk <= $#$tbl; $blk++)
1080    {
1081      next unless defined $tbl->[$blk];
1082      for (my $i = 0, my $br = 0; $i < 256; $br = ++$i % 8)
1083      {
1084        my $code = $tbl->[$blk]->[$i];
1085        print OUTFILE pack($endian,
1086          defined $code ? ($code == $TmpLost ? $LostCode : $code) : $InvCode);
1087      }
1088    }
1089  }
1090
1091  if ($Source)
1092  {
1093    if ($_[0] eq "to_ucs")
1094    {
1095      print OUTFILE
1096"
1097};
1098
1099#endif /* $GuardToUCS && !$GuardSize */
1100
1101";
1102    }
1103    else
1104    {
1105      print OUTFILE
1106"
1107};
1108
1109#endif /* $GuardFromUCS && !$GuardSize */
1110
1111";
1112    }
1113  }
1114}
1115
1116# =============================================================================
1117#
1118# Output 16bit Size-optimized table. Output table's source code if $Source
1119# and table's binary data if !$Source.
1120#
1121# Parameter 1:
1122#    "to_ucs"   - Output "to_ucs" table.
1123#    "from_ucs" - Output "from_ucs" table.
1124# Parameter 2:    Not used when sources are output. Output BE binary if 'n' and
1125#                 LE binary if 'v'.
1126#
1127# =============================================================================
1128sub OutputSize($;$)
1129{
1130  my $endian = $_[1];
1131  my $tbl;
1132  my ($direction, $optimiz, $e, $bytes);
1133  $optimiz = $Bits == 16 ? " size-optimized" : "";
1134  $e = $endian ? ($endian eq 'n' ? " Big Endian" : " Little Endian") : "";
1135  if ($_[0] eq "to_ucs")
1136  {
1137    $tbl = \@ToSizeTbl;
1138    $direction = " $CCSName -> UCS";
1139    $bytes = $ToSizeBytes;
1140
1141    if ($Source)
1142    {
1143      print OUTFILE
1144"/*
1145 * 16-bit $CCSName -> UCS size-optimized table ($ToSizeBytes bytes).
1146 * $Separator
1147 */
1148#if defined ($GuardToUCS) \\
1149 && ($GuardSize)
1150
1151static const __uint16_t
1152${VarToUCSSize}\[] =
1153{
1154";
1155    }
1156  }
1157  elsif ($_[0] eq "from_ucs")
1158  {
1159    $tbl = \@FromSizeTbl;
1160    $direction = " UCS -> $CCSName";
1161    $bytes = $FromSizeBytes;
1162    if ($Source)
1163    {
1164      print OUTFILE
1165"/*
1166 * 16-bit UCS -> $CCSName size-optimized table ($FromSizeBytes bytes).
1167 * $Separator
1168 */
1169
1170#if defined ($GuardFromUCS) \\
1171 && ($GuardSize)
1172
1173static const __uint16_t
1174${VarFromUCSSize}\[] =
1175{
1176";
1177    }
1178  }
1179  else
1180  {
1181    Err "Internal script error  Output16bitSize()\n";
1182  }
1183
1184  printf "Output%s 16-bit%s%s table (%d bytes).\n",
1185  $e, $direction, $optimiz, $bytes if $Verbose;
1186
1187  # OUTPUT FIRST 3 ELEMENTS
1188  if ($Source)
1189  {
1190    printf OUTFILE "\t0x%.4X, /* Ranges number */\n", $tbl->[0];
1191    printf OUTFILE "\t0x%.4X, /* Unranged codes number */\n", $tbl->[1];
1192    printf OUTFILE "\t0x%.4X, /* First unranged code index */\n", $tbl->[2];
1193  }
1194  else
1195  {
1196    printf OUTFILE pack $endian, $tbl->[0];
1197    printf OUTFILE pack $endian, $tbl->[1];
1198    printf OUTFILE pack $endian, $tbl->[2];
1199  }
1200
1201  my $idx = 0;
1202  # OUTPUT RANGES
1203  if ($Source)
1204  {
1205    print OUTFILE "\t/* Ranges list: first code, last Code, array index. */\n";
1206    for (my $range = 0; $range <= $#{$tbl->[3]}; $range++)
1207    {
1208      printf OUTFILE "\t/* Array index: 0x%.4X */ 0x%.4X, 0x%.4X, 0x%.4X,\n",
1209             $idx += 3,
1210             $tbl->[3]->[$range]->[0],
1211             $tbl->[3]->[$range]->[1],
1212             $tbl->[3]->[$range]->[2];
1213    }
1214  }
1215  else
1216  {
1217    for (my $range = 0; $range <= $#{$tbl->[3]}; $range++)
1218    {
1219      print OUTFILE pack($endian, $tbl->[3]->[$range]->[0]),
1220                    pack($endian, $tbl->[3]->[$range]->[1]),
1221                    pack($endian, $tbl->[3]->[$range]->[2]);
1222    }
1223  }
1224  $idx += 3;
1225
1226  # OUTPUT RANGES CONTENT
1227  if ($Source)
1228  {
1229    print OUTFILE "\t/* Ranges content */";
1230    for (my $range = 0; $range <= $#{$tbl->[3]}; $range++)
1231    {
1232      printf OUTFILE "\n\t/* Range 0x%.4X - 0x%.4X, array index: 0x%.4X */",
1233             $tbl->[3]->[$range]->[0], $tbl->[3]->[$range]->[1], $idx;
1234             $idx += $tbl->[3]->[$range]->[1] - $tbl->[3]->[$range]->[0] + 1;
1235      for (my $elt = 0, my $br = 0;
1236           $elt <= $#{$tbl->[4]->[$range]};
1237           $br = ++$elt % 8)
1238      {
1239        print OUTFILE "\n\t" unless $br;
1240        if (defined $tbl->[4]->[$range]->[$elt])
1241        {
1242          if ($tbl->[4]->[$range]->[$elt] != $TmpLost)
1243          {
1244            printf OUTFILE "0x%.4X,", $tbl->[4]->[$range]->[$elt];
1245          }
1246          else
1247          {
1248            print OUTFILE "$MacroLostCode,";
1249          }
1250        }
1251        else
1252        {
1253          print OUTFILE "$MacroInvCode,";
1254        }
1255      }
1256    }
1257  }
1258  else
1259  {
1260    for (my $range = 0; $range <= $#{$tbl->[3]}; $range++)
1261    {
1262      for (my $elt = 0; $elt <= $#{$tbl->[4]->[$range]}; $elt++)
1263      {
1264        if (defined $tbl->[4]->[$range]->[$elt])
1265        {
1266          if ($tbl->[4]->[$range]->[$elt] != $TmpLost)
1267          {
1268            print OUTFILE pack $endian, $tbl->[4]->[$range]->[$elt];
1269          }
1270          else
1271          {
1272            print OUTFILE pack $endian, $LostCode;
1273          }
1274        }
1275        else
1276        {
1277          print OUTFILE pack $endian, $InvCode;
1278        }
1279      }
1280    }
1281  }
1282
1283  # OUTPUT UNRANGED CODES
1284  if ($Source)
1285  {
1286    printf OUTFILE "\n\t/* Unranged codes (%d codes) */", $#{$tbl->[4]} + 1;
1287    for (my $i = 0; $i <= $#{$tbl->[5]}; $i++)
1288    {
1289      printf OUTFILE "\n\t/* Array index: 0x%.4X */ 0x%.4X,0x%.4X,",
1290             $idx, $tbl->[5]->[$i]->[0], $tbl->[5]->[$i]->[1];
1291    }
1292  }
1293  else
1294  {
1295    for (my $i = 0; $i <= $#{$tbl->[5]}; $i++)
1296    {
1297      print OUTFILE pack($endian, $tbl->[5]->[$i]->[0]),
1298                    pack($endian, $tbl->[5]->[$i]->[1]);
1299    }
1300  }
1301
1302  if ($Source)
1303  {
1304    if ($_[0] eq "to_ucs")
1305    {
1306      print OUTFILE
1307"
1308};
1309
1310#endif /* $GuardToUCS && $GuardSize */
1311
1312";
1313    }
1314    else
1315    {
1316      print OUTFILE
1317"
1318};
1319
1320#endif /* $GuardFromUCS && $GuardSize */
1321
1322";
1323    }
1324  }
1325}
1326
1327
1328# =============================================================================
1329#
1330# Parse command line options
1331#
1332# =============================================================================
1333sub ProcessOptions()
1334{
1335  my $help_opt    = 'h'; # Print help option
1336  my $input_opt   = 'i'; # Input file name option
1337  my $output_opt  = 'o'; # Output file name option
1338  my $source_opt  = 'S'; # Generate C source file option
1339  my $enc_opt     = 'N'; # Encoding name
1340  my $plane_opt   = 'p'; # Plane number
1341  my $verbose_opt = 'v'; # Verbose output
1342  my $ccscol_opt  = 'x'; # Encoding's column number
1343  my $ucscol_opt  = 'y'; # UCS column number
1344  my $nosize_opt  = 'l'; # Don't generate size-optimized tables
1345  my $nospeed_opt = 'b'; # Don't generate speed-optimized tables
1346  my $nobe_opt    = 'B'; # Don't generate big-endian tables
1347  my $nole_opt    = 'L'; # Don't generate big-endian tables
1348  my $noto_opt    = 't'; # Don't generate "to_ucs" table
1349  my $nofrom_opt  = 'f'; # Don't generate "from_ucs" table
1350
1351  my %args;              # Command line arguments found by getopts()
1352
1353  my $getopts_string =
1354     "$help_opt$source_opt$enc_opt:$verbose_opt$input_opt:$output_opt:$plane_opt:"
1355   . "$nosize_opt$nospeed_opt$nobe_opt$nole_opt$noto_opt$nofrom_opt$ccscol_opt:"
1356   . "$ucscol_opt:";
1357
1358  getopts($getopts_string, \%args) || Err "getopts() failed: $!.\n", 1;
1359
1360  # Print usage rules and exit.
1361  if ($args{$help_opt})
1362  {
1363    print<<END
1364Usage:
1365     -$help_opt - this help message;
1366     -$input_opt - input file name (required);
1367     -$output_opt - output file name;
1368     -$enc_opt - CCS or encoding name;
1369     -$plane_opt - plane number (high 16 bits) to use (in hex);
1370     -$source_opt - generate C source file;
1371     -$nospeed_opt - don't generate speed-optimized tables (binary files only);
1372     -$nosize_opt - don't generate size-optimized tables (binary files only);
1373     -$nobe_opt - don't generate Big Endian tables (binary files only);
1374     -$nole_opt - don't generate Little Endian tables (binary files only);
1375     -$noto_opt - don't generate "to_ucs" table;
1376     -$nofrom_opt - don't generate "from_ucs" table;
1377     -$ccscol_opt - encoding's column number;
1378     -$ucscol_opt - UCS column number;
1379     -$verbose_opt - verbose output.
1380
1381If output file name isn't specified, <infile>.c (for sources) or
1382<infile>.cct (for binaries) is assumed.
1383If encoding name isn't specified <infile> is assumed.
1384<infile> is normalized (small letters, "-" are substituted by "_") input file
1385name base (no extension). For example, for Koi8-r.txt input file, <infile>
1386is koi8_r.
1387END
1388;
1389    exit 0;
1390  }
1391
1392  $Verbose   = $args{$verbose_opt};
1393  $Source    = $args{$source_opt};
1394  $NoSpeed   = $args{$nospeed_opt};
1395  $NoSize    = $args{$nosize_opt};
1396  $NoBE      = $args{$nobe_opt};
1397  $NoLE      = $args{$nole_opt};
1398  $NoFrom    = $args{$nofrom_opt};
1399  $NoTo      = $args{$noto_opt};
1400  $CCSCol    = $args{$ccscol_opt};
1401  $UCSCol    = $args{$ucscol_opt};
1402  $Plane     = $args{$plane_opt};
1403  $InFile    = $args{$input_opt};
1404  $OutFile   = $args{$output_opt};
1405  $CCSName   = $args{$enc_opt};
1406
1407  Err "Error: input file isn't defined. Use -$help_opt for help.\n", 1
1408  unless $InFile;
1409
1410  unless ($OutFile)
1411  {
1412    # Construct output file name
1413    $OutFile = $InFile;
1414    $OutFile =~ s/(.*\/)*([0-9a-zA-Z-_]*)(\..*)$/\L$2/;
1415    $OutFile =~ tr/-/_/;
1416    if ($Source)
1417    {
1418      $OutFile = "$OutFile.c";
1419    }
1420    else
1421    {
1422      $OutFile = "$OutFile.cct"
1423    }
1424  }
1425
1426  unless ($CCSName)
1427  {
1428    # Construct CCS name
1429    $CCSName = $InFile;
1430    $CCSName =~ s/(.*\/)*([0-9a-zA-Z-_]*)(\..*)$/\L$2/;
1431    $CCSName =~ tr/-/_/;
1432  }
1433
1434  Err "-$nosize_opt option can't be used with -$nospeed_opt option "
1435    . "simultaniously.\n", 1 if $NoSpeed && $NoSize;
1436
1437  Err "-$nobe_opt option can't be used with -$nole_opt option "
1438    . "simultaniously.\n", 1 if $NoBE && $NoLE;
1439
1440  Err "-$noto_opt option can't be used with -$nofrom_opt option"
1441    . "simultaniously.\n", 1 if $NoTo && $NoFrom;
1442
1443  Err "-$nosize_opt, -$nospeed_opt, -$nobe_opt -$nole_opt "
1444    . "-$noto_opt and -$nofrom_opt "
1445    . "options can't be used with -$source_opt option.\n"
1446    . "Source code always contains both speed- and size-optimized "
1447    . "tables in System Endian. Use -$help_opt for help.\n", 1
1448  if $Source and $NoSpeed || $NoSize || $NoBE || $NoLE || $NoTo || $NoFrom;
1449
1450  if (!$CCSCol && !$UCSCol)
1451  {
1452    $CCSCol = 0;
1453    $UCSCol = 1;
1454  }
1455  elsif ($CCSCol && $UCSCol)
1456  {
1457    Err "Column number should be >= 0\n", 1 if ($CCSCol <= 0 or $UCSCol <= 0);
1458    $CCSCol -= 1;
1459    $UCSCol -= 1;
1460  }
1461  else
1462  {
1463    Err "Please, define both CCS and UCS column numbers\n", 1;
1464  }
1465
1466  if ($Verbose)
1467  {
1468    print  "Use $InFile file for input.\n",
1469           "Use $OutFile file for output.\n",
1470           "Use $CCSName as CCS name.\n";
1471    print  "Generate C source file.\n"                if $Source;
1472    print  "Generate binary file.\n"                  if !$Source;
1473    printf "Use plane N 0x%.4X.\n", hex $Plane if defined $Plane;
1474    printf "Use column N $CCSCol for $CCSName.\n";
1475    printf "Use column N $UCSCol for UCS.\n";
1476    print  "Don't generate size-optimized tables.\n"  if $NoSize;
1477    print  "Don't generate speed-optimized tables.\n" if $NoSpeed;
1478    print  "Don't generate big-endian tables.\n"      if $NoBE;
1479    print  "Don't generate little-endian tables.\n"   if $NoLE;
1480    print  "Don't generate \"to_ucs\" table.\n"       if $NoTo;
1481    print  "Don't generate \"from_ucs\" table.\n"     if $NoFrom;
1482  }
1483
1484  return;
1485}
1486
1487
1488# =============================================================================
1489#
1490# Print error message, close all and exit
1491#
1492# Parameter 1: error message
1493# Parameter 2: don't delete output file if > 1
1494#
1495# =============================================================================
1496sub Err($;$)
1497{
1498  print STDERR "$_[0]";
1499  close INFILE;
1500  close OUTFILE;
1501  unlink $OutFile unless $_[1];
1502
1503  exit 1;
1504}