#! /bin/ksh

#
# Name        : ${HOME}/bin/MakeTable.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : Create a (1-byte) translation table
#
# 1.0   First Version
#

\whence set_debug 1>/dev/null 2>&1 && eval $( set_debug ${0##*/} )

typeset FROM=''
typeset TO=''
typeset -x CCC_EXTRA=''

# Code page 500=IBM EBCDIC International, Belgian, Swiss German
# Code page 850=Latin Alphabet Number 1; Latin-1 countries
case "${UNAME:-$( uname -s )}" in
  AIX) TO='IBM-850'; FROM='IBM-500' ;;
  *)   TO='8859_1';  FROM='500' ;;
esac
CCC_EXTRA="-DEXTERNAL_TABLE_TXT=\"From_${FROM}_to_${TO}\""

\cd "${HOME}/src"

\cat > MakeTable.c << __EOF__
#include <stdio.h>
int main()
{
  int c;
  for( c = 0; c < 256; c++ ) 
    putc( c, stdout );
  return 0;  
}
__EOF__
\cat > CheckTable.c << __EOF__
#include <stdio.h>
#include "translation_table.h"
int main()
{
  int i, j;
  for( i = 0; i < 256; i++ ) {
    for( j = 0; j < 256; j++ ) {
      if( translation_table[j] == i )
        break;
    }
    if( j == 256 ) {
      printf( "value 0x%02x not in table.\n", i );
    }
  }
  for( i = 0; i < 256; i++ ) {
    for( j = i + 1; j < 256; j++ ) {
      if( translation_table[i] == translation_table[j] ) {
        printf( "Entry 0x%02x and 0x%02x both have value 0x%02x.\n",
	  i, j, translation_table[i] );
      }
    }
  }
}
__EOF__
cc -o ${HOME}/${UNAME}/MakeTable MakeTable.c \
&& ${HOME}/${UNAME}/MakeTable > MakeTable.txt \
&& iconv -f "${FROM}" -t "${TO}" MakeTable.txt > MakeTable.table \
&& xxd MakeTable.table \
| cut -c10-48 \
| sed -e 's/\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)/0x\1, 0x\2,/g; $s/,$//g' > MakeTable.h \
&& print 'static unsigned char translation_table[256] = {' > translation_table.h \
&& \cat MakeTable.h >> translation_table.h \
&& print '};' >> translation_table.h \
&& cc -o ${HOME}/${UNAME}/CheckTable CheckTable.c \
&& CheckTable
(( $? != 0 )) && exit 1

ccc ebcdic2ascii.c \
&& ccc ebcdicLinedata2ascii.c
exit $?

################# EOF #################
