#!/usr/bin/perl -w
# $XTermId: make-tables,v 1.8 2025/09/12 20:09:37 tom Exp $
# -----------------------------------------------------------------------------
# Copyright 2013,2025 by Thomas E. Dickey
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the above listed
# copyright holder(s) not be used in advertising or publicity pertaining
# to distribution of the software without specific, written prior
# permission.
#
# THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE
# LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# -----------------------------------------------------------------------------
# Convert ".enc" files into C code, using that as a fallback when iconv lacks
# information for a given encoding.

our @table_names;
our $indent = "    ";
our $starts = "/* *INDENT-OFF* */";

sub trim_enc($) {
    my $name = $_[0];
    $name =~ s/\.enc$//;
    return $name;
}

sub table_name($) {
    my $name = &trim_enc( $_[0] );
    $name =~ s/[-.]/_/g;
    return $name;
}

sub do_file($) {
    my $name = $_[0];
    my $n    = $#table_names + 1;
    $table_names[ $n++ ] = $name;
    my $table = &table_name($name);
    printf <<EOF;
$starts
static const BuiltInMapping $table\[\] =
{
EOF
    open( FP, $name ) || die "cannot open $name";
    my (@input) = <FP>;
    close(FP);
    $starts = "";

    my $ignore = 1;
    my $comment;
    my $source;
    my $target;
    my $converted = 0;
    for $n ( 0 .. $#input ) {
        chomp $input[$n];
        if ( $input[$n] =~ /startmapping\s+unicode/i ) {
            $ignore = 0;
        }
        elsif ( $input[$n] =~ /endmapping\s+unicode/i ) {
            $ignore = 1;
        }
        my $comment = $input[$n];
        if ( $comment =~ /#/ ) {
            $comment =~ s/^[^#]*#\s*//;
        }
        else {
            $comment = "";
        }
        if ($ignore) {
            printf "%s/* %s */\n", $indent, $comment if ( $comment ne "" );
        }
        elsif ( $input[$n] =~ /^\s*0x[[:xdigit:]]+/i ) {
            $source = $input[$n];
            $source =~ s/#.*//;
            $source =~ s/^\s+//;
            $target = $source;
            $source =~ s/\s+.*//;
            $target =~ s/^[^[:blank:]]+\s+//;
            $target =~ s/\s+.*//;
            if ( $comment ne "" ) {
                printf "%s{%s, %s},\t/* %s */\n", $indent, $source, $target,
                  $comment;
            }
            else {
                printf "%s{%s, %s},\n", $indent, $source, $target;
            }
            $converted++;
        }
    }
    if ( $converted == 0 ) {
        printf "%s{0, 0},\t/* empty table is illegal syntax */\n", $indent;
    }

    printf <<EOF;
};
EOF
}

sub begin_file() {
    printf <<EOF;
/*
 * \$XTermId\$
 * This file was generated by $0
 */
#include <other.h>
#include <sys.h>
#include <luitconv.h>
EOF
}

sub quoted($) {
    my $value = shift;
    return sprintf( "\"%s\",", $value );
}

sub end_file() {
    my $name;
    printf <<EOF;

#define DATA(name) name, SizeOf(name)
const BuiltInCharsetRec builtin_encodings[] =
{
EOF
    for $name ( sort @table_names ) {
        printf "%s{ %-17s DATA(%s) },\n", $indent, &quoted( &trim_enc($name) ),
          &table_name($name);
    }

    printf <<EOF;
${indent}{ NULL, NULL, 0 }
};
/* *INDENT-ON* */
EOF
}

&begin_file;
while ( $#ARGV >= 0 ) {
    &do_file( shift @ARGV );
}
&end_file;
