% This is the file CEFconv.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{CEFconv.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{
    CEFconv}{
    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}}


% change the category code of all characters possibly used in two byte
%     encodings to `other' so they can be read.

\@tempcnta "0A1
\loop
  \catcode\@tempcnta 12
  \ifnum\@tempcnta < "0FE
    \advance\@tempcnta \@ne
\repeat

% the next characters must be enabled too; 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 TeX control characters get
%     the category `other', therefore 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{
        CEFconv}{
        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) macro \CNSpreproc at the beginning of the output to
%     signal CNS.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{}}
    \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

\endlinechar `\^^M

\@@end
