% This is the file CEF5conv.tex of the CJK package
%   for using asiatic logographs (Chinese/Japanese/Korean) with LaTeX2e
%
% created by Werner Lemberg <a7621gac@awiuni11.bitnet>
%
% Version 2.5 (10-Apr-1995)
%
%
% ATTENTION! This `program' will fail under the web2c-TeX implementation
%            because \write-commands will not output 8bit characters.
%            It will work with emTeX if the iniTeX options -8 (8bit input) and
%            -o (8bit output) are set. Other TeX implementations are not
%            tested.
%
%            In the utils/CEFconv directory you can find an equivalent
%            C program which does the same job.

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\def\fileversion{2.5}
\def\filedate{1995/04/10}
\ProvidesFile{CEF5conv.tex}[\filedate\space\fileversion]


\makeatletter
\endlinechar \m@ne


\ifx\CEFin \@undefined
  \def\CEFin{CEFinput.tex}
\else
  \ifx\CEFin \@empty
    \def\CEFin{CEFinput.tex}
  \fi
\fi

\ifx\CEFout \@undefined
  \def\CEFout{CEFinput.cjk}
\else
  \ifx\CEFout \@empty
    \def\CEFout{CEFinput.cjk}
  \fi
\fi

\ifx\CEFin \CEFout
  \PackageError{
    CEF5conv}{
    Input and output file must not be identical}{}
  \@@end
\fi


\newif\ifCEF@continue@
\newif\ifCEF@again@

\newread\CEF@inFile
\newwrite\CEF@outFile

% the use of token register prevents expansion of the activated input line
%     characters.

\newtoks\CEF@outLine
\newtoks\CEF@pre
\newtoks\CEF@post


% we need a non-active ampersand for the definition of \CEF@scanLine;
%     this macro simply splits its argument into four pieces; if there is
%     no CEF macro, \CEF@post will be empty.

\@makeother{\&}

\def\CEF@stop{\CEF@stop}

\def\CEF@scanLine#1&C#2-#3;#4\CEF@stop{
  \CEF@pre{#1}
  \def\CEF@plane{#2}
  \def\CEF@code{#3}
  \CEF@post{#4}}


% make all characters possibly used as the first byte active so they can
%     become macros.

\def\CEF@active{
  \@tempcnta "0A1
  \loop
    \catcode\@tempcnta \active
    \ifnum\@tempcnta < "0FE
      \advance\@tempcnta \@ne
  \repeat}

% each Big 5 code `XY' will be replaced with `XZZZ.'; ZZZ is the decimal
%     representation of Y.

\def\CEF@assign#1{
  \def#1##1{
    \string#1\number`##1.}}

% we now activate all possible first bytes of a Big 5 code inside of a group
%     and assign the above defined macro to them.

\begingroup
  \CEF@active

  \CEF@assign{^^a1}
  \CEF@assign{^^a2}
  \CEF@assign{^^a3}
  \CEF@assign{^^a4}
  \CEF@assign{^^a5}
  \CEF@assign{^^a6}
  \CEF@assign{^^a7}
  \CEF@assign{^^a8}
  \CEF@assign{^^a9}
  \CEF@assign{^^aa}
  \CEF@assign{^^ab}
  \CEF@assign{^^ac}
  \CEF@assign{^^ad}
  \CEF@assign{^^ae}
  \CEF@assign{^^af}
  \CEF@assign{^^b0}
  \CEF@assign{^^b1}
  \CEF@assign{^^b2}
  \CEF@assign{^^b3}
  \CEF@assign{^^b4}
  \CEF@assign{^^b5}
  \CEF@assign{^^b6}
  \CEF@assign{^^b7}
  \CEF@assign{^^b8}
  \CEF@assign{^^b9}
  \CEF@assign{^^ba}
  \CEF@assign{^^bb}
  \CEF@assign{^^bc}
  \CEF@assign{^^bd}
  \CEF@assign{^^be}
  \CEF@assign{^^bf}
  \CEF@assign{^^c0}
  \CEF@assign{^^c1}
  \CEF@assign{^^c2}
  \CEF@assign{^^c3}
  \CEF@assign{^^c4}
  \CEF@assign{^^c5}
  \CEF@assign{^^c6}
  \CEF@assign{^^c7}
  \CEF@assign{^^c8}
  \CEF@assign{^^c9}
  \CEF@assign{^^ca}
  \CEF@assign{^^cb}
  \CEF@assign{^^cc}
  \CEF@assign{^^cd}
  \CEF@assign{^^ce}
  \CEF@assign{^^cf}
  \CEF@assign{^^d0}
  \CEF@assign{^^d1}
  \CEF@assign{^^d2}
  \CEF@assign{^^d3}
  \CEF@assign{^^d4}
  \CEF@assign{^^d5}
  \CEF@assign{^^d6}
  \CEF@assign{^^d7}
  \CEF@assign{^^d8}
  \CEF@assign{^^d9}
  \CEF@assign{^^da}
  \CEF@assign{^^db}
  \CEF@assign{^^dc}
  \CEF@assign{^^dd}
  \CEF@assign{^^de}
  \CEF@assign{^^df}
  \CEF@assign{^^e0}
  \CEF@assign{^^e1}
  \CEF@assign{^^e2}
  \CEF@assign{^^e3}
  \CEF@assign{^^e4}
  \CEF@assign{^^e5}
  \CEF@assign{^^e6}
  \CEF@assign{^^e7}
  \CEF@assign{^^e8}
  \CEF@assign{^^e9}
  \CEF@assign{^^ea}
  \CEF@assign{^^eb}
  \CEF@assign{^^ec}
  \CEF@assign{^^ed}
  \CEF@assign{^^ee}
  \CEF@assign{^^ef}
  \CEF@assign{^^f0}
  \CEF@assign{^^f1}
  \CEF@assign{^^f2}
  \CEF@assign{^^f3}
  \CEF@assign{^^f4}
  \CEF@assign{^^f5}
  \CEF@assign{^^f6}
  \CEF@assign{^^f7}
  \CEF@assign{^^f8}
  \CEF@assign{^^f9}
  \CEF@assign{^^fa}
  \CEF@assign{^^fb}
  \CEF@assign{^^fc}
  \CEF@assign{^^fd}
  \CEF@assign{^^fe}

% the next characters must be enabled; they identify translated planes of CNS
%     codes.

  \@makeother{^^80}
  \@makeother{^^81}
  \@makeother{^^82}
  \@makeother{^^83}
  \@makeother{^^84}
  \@makeother{^^85}
  \@makeother{^^86}
  \@makeother{^^87}

% we process all CEF macros in a loop. Notice that \CEF@scanLine was called
%     once when we enter \CEF@buildOutLine, thus the input line is already
%     split.
%
% The processed part of the input line will be accumulated in the register
%     \CEF@outLine; each CEF macro will be replaced with a hexadecimal
%     character in the range 0x80-0x87 followed by a hexadecimal string, a
%     dot, a hexadecimal number, and a dot again. The macro \CEF@addCode does
%     this and appends the result to \CEF@outLine.
%
% If \CEF@post is empty the loop will be exited, otherwise it is split again.

  \def\CEF@buildOutLine{
    \CEF@outLine{}
    \CEF@again@true
    \loop
      \ifcase\CEF@plane
            \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^80}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^81}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^82}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^83}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^84}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^85}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^86}
        \or \CEF@outLine\expandafter\expandafter\expandafter{
             \expandafter\the\expandafter\CEF@outLine\the\CEF@pre ^^87}
      \fi
      \expandafter\CEF@addCode \CEF@code

      \expandafter\CEF@scanLine \the\CEF@post\CEF@stop
      \edef\CEF@temp{\the\CEF@post}
      \ifx\CEF@temp \@empty
        \CEF@again@false
        \CEF@outLine\expandafter\expandafter\expandafter{
          \expandafter\the\expandafter\CEF@outLine\the\CEF@pre}
      \fi

    \ifCEF@again@
    \repeat}

  \def\CEF@addCode#1#2#3#4{
    \uppercase{\edef\CEF@temp{#1#2."0#3#4.}}
    \CEF@outLine\expandafter\expandafter\expandafter{
      \expandafter\the\expandafter\CEF@outLine\CEF@temp}}


% the major work is done by \CEF@convertInput. All characters (except Big 5
%     characters) get the category `other'.
%
% because of changing all category codes to `active' or `other', we first
%     define the macro and execute it afterwards.

  \def\CEF@convertInput{
    \begingroup
      \immediate\openin\CEF@inFile\CEFin\relax
      \immediate\openout\CEF@outFile\CEFout\relax
      \ifeof\CEF@inFile
        \PackageError{
          CEF5conv}{
          Can't open \CEFin}{}
        \@@end
      \fi

      \@makeother{\\}
      \@makeother{\{}
      \@makeother{\}}
      \@makeother{\$}
%     \@makeother{\&}       % already done
      \@makeother{\^^K}
      \@makeother{\#}
      \@makeother{\^}
      \@makeother{\_}
      \@makeother{\ }
      \@makeother{\%}
      \@makeother{\^^A}
      \@makeother{\~}

% we define the (empty) macros \CNSpreproc and \CJKproproc at the beginning of
%     the output to signal CNS.sty and CJK.sty a preprocessed input file.
%
% if \CEF@post is empty after a call of \CEF@scanLine the input line is
%     written out, otherwise \CEF@buildOutLine processes the input further.

      \immediate\write\CEF@outFile{\string\def\string\CNSpreproc{}}
      \immediate\write\CEF@outFile{\string\def\string\CJKpreproc{}}
      \CEF@continue@true
      \loop
        \read\CEF@inFile to \CEF@inLine
        \ifeof\CEF@inFile
          \CEF@continue@false
        \else
          \expandafter\CEF@scanLine \CEF@inLine &C-;\CEF@stop
          \edef\CEF@temp{\the\CEF@post}
          \ifx\CEF@temp \@empty
            \immediate\write\CEF@outFile{\CEF@inLine}
          \else
            \begingroup
              \CEF@buildOutLine
              \edef\CEF@temp{\the\CEF@outLine}
              \immediate\write\CEF@outFile{\CEF@temp}
            \endgroup
          \fi
        \fi
      \ifCEF@continue@
      \repeat

      \immediate\closein\CEF@inFile
      \immediate\closeout\CEF@outFile
    \endgroup}

  \CEF@convertInput
\endgroup

\endlinechar `\^^M

\@@end
