% This is the file Bg5conv.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/Bg5conv 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{Bg5conv.tex}[\filedate\space\fileversion]


\makeatletter
\endlinechar \m@ne


\ifx\CJKin \@undefined
  \def\CJKin{Bg5input.tex}
\else
  \ifx\CJKin \@empty
    \def\CJKin{Bg5input.tex}
  \fi
\fi

\ifx\CJKout \@undefined
  \def\CJKout{Bg5input.cjk}
\else
  \ifx\CJKout \@empty
    \def\CJKout{Bg5input.cjk}
  \fi
\fi

\ifx\CJKin \CJKout
  \PackageError{
    Bg5conv}{
    Input and output file must not be identical}{}
  \@@end
\fi


\newif\ifCJK@continue@

\newread\CJK@inFile
\newwrite\CJK@outFile


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

\def\CJK@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\CJK@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
  \CJK@active

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

% the real work is done by \CJK@convertInput. All characters (except Big 5
%     characters) get the category `other'; then simply the input file is
%     read line by line into \CJK@inLine and written out.
%
% because of changing all category codes to `active' or `other', we first
%     define the macro and execute it afterwards.

  \def\CJK@convertInput{
    \begingroup
      \immediate\openin\CJK@inFile\CJKin\relax
      \immediate\openout\CJK@outFile\CJKout\relax
      \ifeof\CJK@inFile
        \PackageError{
          Bg5conv}{
          Can't open \CJKin}{}
        \@@end
      \fi

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

% we define the (empty) macro \CJKpreproc at the beginning of the output to
%     signal CJK.sty a preprocessed input file.

      \immediate\write\CJK@outFile{\string\def\string\CJKpreproc{}}
      \CJK@continue@true
      \loop
        \read\CJK@inFile to\CJK@inLine
        \ifeof\CJK@inFile
          \CJK@continue@false
        \else
          \immediate\write\CJK@outFile{\CJK@inLine}
        \fi
      \ifCJK@continue@
      \repeat

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

  \CJK@convertInput
\endgroup

\endlinechar `\^^M

\@@end
